Click to share! ⬇️

Python JSON Tutorial

Working with data is the whole point of using Python as a programming language. JSON is perhaps the most common data format, and we’ll take a look at working with JSON using Python now. Python can handle processing JSON from a collection of possible data sources like files, Web APIs, or third party modules that output JSON. Python has a built-in module for JSON processing which is part of the Python standard library.

Python JSON Parsing Functions

  • obj = load(file)
  • obj = loads(string)

Python JSON Serialization Functions

  • dump(obj, file)
  • str = dumps(obj)

Serializing Python Data To JSON

Python converts data to JSON as shown below in the table. Python Dictionary objects are converted into JSON objects. Lists and Tuples are converted into arrays. Python Strings are converted into JavaScript strings. The numeric types in Python that are derived from integers and floating-point numbers are parsed into numbers. Boolean True and False are converted into their counterparts in JavaScript, and the Python value of None is converted to null in JSON.

Python Object JSON Representation
dict object
list, tuple array
str string
int, long, float, Enums number
True true
False false
None null

Parsing JSON into Python

Parsing JSON into Python is pretty much the opposite of the serialization steps noted above. The one exception for that is dealing with lists and tuples. The reason for this is because lists and tuples in Python are encoded to arrays in JSON. This means when you parse the list back into Python, there’s no way to tell whether it was a list or a tuple originally. You could take a Python object, serialize it to JSON, parse it back into Python, and have a different object than you started with. The takeaway is that when parsing JSON into Python, you’ll never end up with a tuple in the Python object, it will always be a list.

JSON Data Python Object
object dict
array list
string str
Integer number int
Floating point number float
true, false True, False
null None

json.loads() Parse Example

Here is some Python code to show using the json.loads() method. To use loads() you first need to import json into the Python file which is pretty easy. All you need to do is type import json right at the start of the file. In the code is a variable named jsonstring, that has a JSON formatted string assigned to it. This data structure represents a fictional order at your favorite Mexican Burrito dealer. This allows us to read that JSON data using json.loads(jsonstring) storing the result into the data variable. The variable jsonstringis of type <class ‘str’> before using the loads() function. Thre resulting type stored in data is of type <class ‘dict’>. Once we have the data in a Python dictionary, you can see how it is very easy to loop over the contents.

import json

jsonstring = '''{
        "burrito" : "Steak",
        "double-meat" : true,
        "toppings" : [
            "Black Beans",
            "Lettuce",
            "Salsa",
            "Guacamole"
        ],
        "price" : 9.17
    }'''

data = json.loads(jsonstring)

print('Order: ' + data['burrito'])
if (data['double-meat']):
    print('With Double Meat')
for topping in data['toppings']:
    print('Topping: ' + topping)
Order: Steak
With Double Meat
Topping: Black Beans
Topping: Lettuce
Topping: Salsa
Topping: Guacamole

json.dumps() Serialize Example

Now we can easily serialize a Python object into a JSON representation. In the code below is a pythondict variable that holds all the information about our Burrito order. This Python dictionary can be converted to a JSON string using the json.dumps() function. We pass the function the data to convert as the first argument, and the number of indent spaces to use as a named second argument. The result of that operation is stored in the jsonstring variable. When we print that variable out, we see a nice JSON string as output. So now we see how to both parse and serialize JSON data using Python.

import json

pythondict = {
    'burrito': 'Steak',
    'double-meat': True,
    'toppings': ['Black Beans',
                 'Lettuce',
                 'Salsa',
                 'Guacamole'
                 ],
    'price': 9.17
}

jsonstring = json.dumps(pythondict, indent=4)

print('-------- JSON String Data --------')
print(jsonstring)
-------- JSON String Data --------
{
    "burrito": "Steak",
    "double-meat": true,
    "toppings": [
        "Black Beans",
        "Lettuce",
        "Salsa",
        "Guacamole"
    ],
    "price": 9.17
}

Handling JSON errors with JSONDecodeError

It is possible to encounter errors when parsing and serializing JSON in Python. To handle these situations we can use the JSONDecodeError class that is part of the JSON module itself. Let’s revisit one of the examples so far and wrap the JSON parsing in a try/except block. We use JSONDecodeError to emit custom error messages if anything goes wrong. The code is highlighted where there is a missing comma and the results from running the code show that this error was caught and handled.

import json
from json import JSONDecodeError

jsonstring = '''{
        "burrito" : "Steak",
        "double-meat" : true,
        "toppings" : [
            "Black Beans",
            "Lettuce"
            "Salsa",
            "Guacamole"
        ],
        "price" : 9.17
    }'''

try:
    data = json.loads(jsonstring)

    print('Order: ' + data['burrito'])
    if (data['double-meat']):
        print('With Double Meat')
    for topping in data['toppings']:
        print('Topping: ' + topping)

except JSONDecodeError as error:
    print('Hold on now, there was a JSON Decoding erroror:')
    print(error.msg)
    print(error.lineno, error.colno)
Hold on now, there was a JSON Decoding erroror:
Expecting ',' delimiter
7 13

Now we have a different error in the JSON.

import json
from json import JSONDecodeError

jsonstring = '''{
        "burrito" : "Steak",
        "double-meat" : true,
        "toppings" : [
            "Black Beans",
            "Lettuce",
            "Salsa",
            "Guacamole
        ],
        "price" : 9.17
    }'''

try:
    data = json.loads(jsonstring)

    print('Order: ' + data['burrito'])
    if (data['double-meat']):
        print('With Double Meat')
    for topping in data['toppings']:
        print('Topping: ' + topping)

except JSONDecodeError as error:
    print('Hold on now, there was a JSON Decoding erroror:')
    print(error.msg)
    print(error.lineno, error.colno)
Hold on now, there was a JSON Decoding erroror:
Invalid control character at
8 23

Working With JSON from an API

We can once again use the familiar httpbin.org site to test out using the Requests Library along with some JSON parsing and serialization. The pattern of retrieving JSON text and parsing it into a native dictionary is common in Python and the Requests library will automatically detect if the returned content from a request is JSON and will parse it for you. In the code below, you can see that we use requests.get(url) to make a GET request to http://httpbin.org/json. That endpoint offers this output.
httpbin json endpoint

The .json() function is a convenience function allowing the developer to quickly access JSON data.

import json, requests

url = 'http://httpbin.org/json'
result = requests.get(url)

pythondict = result.json()

print(json.dumps(pythondict, indent=4))

print(list(pythondict.keys()))

print(pythondict['slideshow']['title'])
slides = len(pythondict['slideshow']['slides'])
print(f'There are {slides} slides')
{
    "slideshow": {
        "author": "Yours Truly",
        "date": "date of publication",
        "slides": [
            {
                "title": "Wake up to WonderWidgets!",
                "type": "all"
            },
            {
                "items": [
                    "Why <em>WonderWidgets</em> are great",
                    "Who <em>buys</em> WonderWidgets"
                ],
                "title": "Overview",
                "type": "all"
            }
        ],
        "title": "Sample Slide Show"
    }
}
['slideshow']
Sample Slide Show
There are 2 slides

Learn More About JSON in Python

Python JSON Tutorial Summary

In this tutorial, we learned how to work with JSON in Python. Some of the key points are summarized here.

  • The loads() function is used to parse JSON code from a string.
  • The load() function is used to parse the JSON code from a file.
  • The JSONDecodeError class is used to handle JSON parsing errors.
  • The dumps() function serializes Python to JSON
  • The dump() function can be passed a second argument saving JSON to a file.
  • To access the parsed JSON code from a request, we can use the json() function.
Click to share! ⬇️