How to Parse JSON Data in Python

Updated: February 20, 2023 By: Goodman Post a comment

This example-based article is about parsing JSON data in Python using a built-in module named json.


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.


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))


<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


import requests
import json

response = requests.get('')
data = json.loads(response.content)



{'success': True, 'message': 'Photos fetched successfully', 'offset': 0, 'limit': 10, 'photos': [{'user': 28, 'id': 1, 'title': 'Defense the travel audience hand', 'url': '', '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)



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().

Notify of
Inline Feedbacks
View all comments