Python JSON – Read JSON data from URL

In this post we will be creating a Python script that will read JSON data from an existing URL, to help us achieve this we will be using the built-in JSON library which allows for easy encoding and decoding of Python objects into JSON format. It can handle various data types such as lists, dictionaries, strings, and numbers. A project around this library would be especially useful in interacting with web APIs that return JSON data.

Our URL for this example can be seen below from JSON placeholder which is a useful site for free fake API data whilst testing code.

https://jsonplaceholder.typicode.com/posts

See the snippet of Python code where we read JSON data from a URL. We start by making a get request to our URL which retrieves our data. We then use the ‘json.load()’ method to deserialize the JSON data and then print this to output.

import json
import requests

response = requests.get("https://jsonplaceholder.typicode.com/posts")

data = json.loads(response.text)

print(data)

Take a look at some of our other content around the Python programming language by clicking here.

Leave a Reply