This example-based article is about parsing JSON data in Python using a built-in module named json.
Overview
The json module in Python provides 4 main methods for working with JSON data:
- json.loads() method is used to parse a JSON string and convert it into a Python object. The resulting Python object from the json.loads() method can be a dictionary, a list, a string, a number, or a Boolean value.
- json.dumps() method is used to convert a Python object into a JSON string.
- json.load() and json.dump() are similar to json.loads() and json.dumps(), respectively, but are used to read and write JSON data to and from files rather than strings.
Now, it’s time for practice. Let’s examine some real-world use cases and write some code.
Examples
Reading a JSON file
One of the most common use cases for working with JSON in Python is to read data from a JSON file. Let’s say we have a file named data.json that stores the following:
{
"description": "Some fictitious products",
"products": [
{
"name": "Apple",
"price": 1.2
},
{
"name": "Orange",
"price": 2.4
}
]
}
We can use Python to load all content of the file and convert it to a dictionary like this:
import json
with open('data.json') as f:
data = json.load(f)
print(type (data))
print(data)
Output:
<class 'dict'>
{
'description': 'Some fictitious products',
'products': [
{'name': 'Apple', 'price': 1.2},
{'name': 'Orange', 'price': 2.4}
]
}
JSON files are often used to persist data (in case it is not necessary or inappropriate to use databases).
Parsing JSON from an API response
Another common use case is to parse JSON data returned by a RESTful API. To get the response from a RESTful API, we will utilize the requests module. It needs to be installed before using:
pip install requests
Example:
import requests
import json
response = requests.get('https://api.slingacademy.com/v1/sample-data/photos')
data = json.loads(response.content)
print(data)
Output:
{'success': True, 'message': 'Photos fetched successfully', 'offset': 0, 'limit': 10, 'photos': [{'user': 28, 'id': 1, 'title': 'Defense the travel audience hand', 'url': 'https://api.slingacademy.com/public/sample-photos/1.jpeg', 'description': 'Leader structure safe or bl
...
Using a custom JSON decoder
The json module provides a default decoder for parsing JSON data. However, you can also create a custom decoder to handle specific cases. In the example below, we will create a custom decoder to parse a JSON file that contains Unix timestamps.
Here’s the JSON file named sling_academy.json that is used in the example:
{
"name": "John Doe",
"email": "[email protected]",
"timestamp": 1699704671
}
The code:
import json
import datetime
class UnixTimestampDecoder(json.JSONDecoder):
def decode(self, s):
obj = super().decode(s)
if isinstance(obj, int):
return datetime.datetime.fromtimestamp(obj)
return obj
with open('sling_academy.json') as f:
data = json.load(f, cls=UnixTimestampDecoder)
print(datetime.datetime.fromtimestamp(data['timestamp']))
Output:
2023-11-11 19:11:11
In this example, the UnixTimestampDecoder class is a custom decoder that converts Unix timestamps to Python datetime objects. The cls parameter is used to specify the custom decoder when calling json.load().