How to Parse JSON Data from a URL in Python?

Estimated read time 1 min read

To parse JSON data from a URL in Python, you can use the requests library to make a GET request to the URL, and then use the json module to parse the JSON data.

Here’s an example:

import requests
import json

url = "https://jsonplaceholder.typicode.com/todos/1"

response = requests.get(url)

if response.status_code == 200:
    json_data = json.loads(response.text)
    print(json_data)
else:
    print("Failed to retrieve data from URL")

In this example, we make a GET request to the URL "https://jsonplaceholder.typicode.com/todos/1", which returns a JSON object. We then check that the response status code is 200 (indicating a successful response), and if it is, we use the json.loads() function to parse the JSON data from the response text. Finally, we print the parsed JSON data to the console. If the response status code is not 200, we print a failure message.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply