track ip addresses, lookup mac addresses, etc

Working with JSON in Python

Published on 30 May 2022

What is JSON?

JSON is a text-based format for representing structured data that is based on JavaScript object syntax. JSON stands for JavaScript Object Notation.

A JSON string contains of a key-value format where keys map to values. We will create a JSON string with three keys: (1) planet name (2) number of moons (3) position from sun.

name will be the first key. The number of moons, denoted by number_of_moons will be the second key. The position from sun, denoted by position will be the third key. This object is denoted in the JSON string below:

{"name": "Saturn", "number_of_moons": 82, "position": 6}

Here, we see that the key name is of string datatype and number_of_moons and position are integer. JSON strings can have different values of different data types. It is similar to a Python dictionary, except that JSON is a standard data format and Python dictionaries are data types. JSON is language agnostic.

Serialization and Deserialization

Before JSON data is sent over the network, it is converted from complex data types or custom-defined classes to native data types in the form of a JSON string. This process is called serialization. After the JSON string is received, the custom class is reconstructed from the JSON strong. This process is called deserialization.

To serialize a Python datatype, we use json.dumps().

Look at this list of dictionaries:

planets = [{"name": "Jupiter", "moon_count": {"official": 53, "pending": 26}},
         {"name": "Saturn", "moon_count": {"official": 53, "pending": 29}}]

If we want to convert this to JSON and send it as JSON, we use json.dumps(). Before that, we first need to import the built-in JSON library with:

import json

Run this on the Python shell:

>>> import json
>>> print(json.dumps(planets))

The output will be:

'[{"name": "Jupiter", "moon_count": {"official": 53, "pending": 26}}, {"name": "Saturn", "moon_count": {"official": 53, "pending": 29}}]'

The function json.dumps() takes a Python list of dictionaries and converts it to a JSON array.

To read more about the json.dumps() function, run this on the Python shell.

>>> help(json)

If you want to convert planets to a JSON array with indentation, add indent=4 to the dumps() method.

serialized_planets = json.dumps(planets, indent=4)
print(serialized_planets)

The output will be:

[
    {
        "name": "Jupiter",
        "moon_count": {
            "official": 53,
            "pending": 26
        }
    },
    {
        "name": "Saturn",
        "moon_count": {
            "official": 53,
            "pending": 29
        }
    }
]

That was neatly formatted with a 4-space indent level.

Now, if we need to reconstruct the Python data type from the serialized_planets, we deserialize it. To deserialize JSON data, we use json.loads().

deserialized_planets = json.loads(serialized_planets)
print(deserialized_planets)

The output will be:

[{'name': 'Jupiter', 'moon_count': {'official': 53, 'pending': 26}}, {'name': 'Saturn', 'moon_count': {'official': 53, 'pending': 29}}]

json.dumps() vs json.loads()

json.dumps() takes in a Python data type and returns a JSON string.

json.loads() takes in a JSON string and returns a Python data type.

Sorting JSON data by keys

Let us create a Python dictionary called fruits which contains key-value pairs of fruit name and quantity. We will create a JSON string called json_fruits from it.

fruits = {
    'mango': 6,
    'apple': 10,
    'orange': 7,
    'banana': 9,
    'grape': 3
}
json_fruits = json.dumps(fruits, indent=4)
print(json_fruits)

The output will be:

{
    "mango": 6,
    "apple": 10,
    "orange": 7,
    "banana": 9,
    "grape": 3
}

If we want the JSON string to have the key-value pairs sorted by key, that is sorted in order of fruit names alphabetically, we use the sorted_keys=True argument.

json_sorted_fruits = json.dumps(fruits, indent=4, sort_keys=True)
print(json_sorted_fruits)

The output will be:

{
    "apple": 10,
    "banana": 9,
    "grape": 3,
    "mango": 6,
    "orange": 7
}

The JSON string is sorted based on the keys.

Matching JSON and Python data types

JSON Python
null None
string str
true True
false False
int int
real float
array list
object dict

Only the basics

We have only covered the basics of JSON handling with Python. Depending on the interest, I will make one or more advanced Python JSON blog posts which will cover encoding and decoding custom types and TypeError error handling in Python, and a practical example of receiving JSON data using the requests module.

Extras

If this is your first time here, we have a few JSON related utilities on this website. This JSON sorter toool sorts a JSON array.

Created on 30 May 2022

TAGS: python, json

← Previous Post ACSL Intermediate Division

Visual Studio Code for Python development Next Post →