This tutorial will show you how to create Python dictionary from JSON data retrieved from an API via HTTP. This is useful for retrieving data to use in your Python scripts and applications.
Step 1: Install the request Library
The easiest way to interact with web resources in Python is using the requests library. Install it by running:
pip install requests
You may want to use a virtual environment to keep the dependencies for all of your Python projects separate.
Step 2: Know the Format of the JSON Data you are Retrieving
JSON data is structured into arrays and objects. To work with it, you’ll need to know the format of the data in advance – how it is arranged, the field names, and types. For this example, we will use the data from the URL:
https://jsonplaceholder.typicode.com/posts/
This is an example JSON formatted endpoint mimicking a REST API, with the data in the following format:
[ { "userId": 1, "id": 1, "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto" }, { "userId": 1, "id": 2, "title": "qui est esse", "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla" } ]
This data contains an array of objects representing blog posts, each with a userId, id, title and body.
The structure and format of your data will differ depending on the purpose or source, but you’ll need to know this format before working with it so that you can correctly access the information stored.
Step 3: Write the Code to Get and Parse JSON
Once you know what the data is meant to look like, you can load it using the requests library:
response = requests.get('https://appmaster.io/blog/rest-api-examples')
The requests library has a built in JSON decoder that does pretty much all of the work:
data = response.json()
The JSON decoder will determine whether the response is an object or array and convert it to a Python dictionary or list as appropriate. You can view the results by running:
print(data)
An access information within the new array or dictionary using indexes and keys as you would any other Python entities:
print(data[0]['title])
Above, the title of the first post (at index 0) is printed.
Step 4: Error Handling
If something goes wrong when loading or processing the JSON data, you’ll want to handle it:
response = requests.get('https://appmaster.io/blog/rest-api-examples') try: data = response.json() firstPost = data[0['title']] except KeyError: print("Error parsing JSON data") firstPost = None except ConnectionError: print("Connection error") firstPost = None print(firstPost)
Above, two errors are caught; KeyError which will be raised if there is an issue accessing the specified key in the data (likely if you arr accessing a key that doesn’t exist, because you are expecting the wrong format), and connectionError, which will be raised if there is a connection problem.
You can view the full list of errors and exceptions here.
Next Steps – Doing more with Python Requests
The requests library can do much, much more than this, so be sure to check out the documentation.