Working with JSON Data in Python

In this article, you will learn how to parse, read, and write JSON Data in Python.

The JSON (JavaScript Object Notation) format is a standard for storing and exchanging data between processes. Json was originally part of JavaScript, but it has since grown, gained its own standard, and currently exists independently of JS or any other programming language. If you want to be a programmer, you’ll come across the json format sooner or later. Json should be one of the first words that springs to mind whenever there is a need for storage (hard disk, database) or data interchange between programs.

In Python, how do you deal with JSON Data?

Serialize and Deserialize operations

Python is simple to learn yet highly powerful, as evidenced by the way it handles json file processing. Using a dynamic type language, as well as two data structures: dict, list, and python, will make handling json feel natural and simple.

Before you go into the intricacies, you first understand how the computer software interacts with the file. You manipulate items in the application. When you wish to store or send data across a network, you must first convert the objects into a format that your computer can handle. Serialize is the term for this procedure.

Deserialize, on the other hand, is a procedure that restores an object from a file or bit sequence received over the network. Simply said, if you wish to handle json files, you must convert the contents of the json files on your hard drive into objects that the Python program recognizes (deserialize). When saving items as json, you must first convert them to a format that the hard disk can save (serialize).

Deserialize JSON Python

Python’s standard library now includes the json module, which was introduced in version 2.6. As a result, if your Python version is greater, you only need to import this module to work with json. If you’re using an older version of Python, you can install this package with pips. However, I strongly advise you to switch to a newer version of Python, as Python 2.x is no longer supported.

import jsonCode language: JavaScript (javascript)

We must transition from json to python type in deserialize. The following is the equivalent conversion table:

JSONPython
objectdict
arraylist
stringstr
number(int)int
number(real)int
trueTrue
falseFalse
nullNone 

The load function is used to deserialize a json file into an object. Consider the following from the people.json file:

with open("people.json", "r") as fin:

    data = json.load(fin)

print(data)

>>> {'name': 'Guido van Rossum', 'age': 65, 'degree': ['mathematics', 'computer science'], 'retired': True, 'carrer': {'google': {'from': 1999, 'to': 2013}, 'dropbox': {'from': 2013, 'to': 2019}}}

print(type(data))

>>> <class 'dict'>

print(type(data['age']))

>>> <class 'int'>

print(type('carrer'))

>>> <class 'dict'>

We can also deserialize a json string, using load functions.

json_string = """

{

 "name": "Guido van Rossum",

 "age": 65,

 "degree": ["mathematics", "computer science"],

 "retired": true,

 "carrer": {

  "google":{

   "from": 1999,

   "to": 2013

  },

  "dropbox": {

   "from": 2013,

   "to": 2019

  }

 }

}

"""

data = json.loads(json_string)

print(data)

>>> {'name': 'Guido van Rossum', 'age': 65, 'degree': ['mathematics', 'computer science'], 'retired': True, 'carrer': {'google': {'from': 1999, 'to': 2013}, 'dropbox': {'from': 2013, 'to': 2019}}}

print(type(data))

<class 'dict'>

After deserializing objects, you can now use them as normal.

print(len(data['degree']))

>>> 2

print(data['degree'][0])

>>> mathematics

print(data['age'] + 3)

>>> 68Code language: JavaScript (javascript)

Serialize Python Objects Into JSON

We must switch from python’s data type to json’s data type during the serialization process. The following is the equivalent conversion table:

PythonJSON
dictobject
list,tuplearray
strstring
Int, long, floatnumber
Truetrue
Falsefalse
Nonenull

The dump function is used to store a Python object as a json file.

data = {'name': 'Guido van Rossum', 'age': 65, 'degree': ['mathematics', 'computer science'], 'retired': True, 'carrer': {'google': {'from': 1999, 'to': 2013}, 'dropbox': {'from': 2013, 'to': 2019}}}

with open("people.json", "w") as fout:
    json.dump(data, fout)Code language: JavaScript (javascript)

Use the dumps function if you only need to convert a python object to a string json. Dumps and dumps also include more indent parameters, allowing you to create more professional-looking outcomes.

data = {'name': 'Guido van Rossum', 'age': 65, 'degree': ['mathematics', 'computer science'], 'retired': True, 'carrer': {'google': {'from': 1999, 'to': 2013}, 'dropbox': {'from': 2013, 'to': 2019}}}

json_string = json.dumps(data)

print(json_string)

>>> {"name": "Guido van Rossum", "age": 65, "degree": ["mathematics", "computer science"], "retired": true, "carrer": {"google": {"from": 1999, "to": 2013}, "dropbox": {"from": 2013, "to": 2019}}}

print(type(json_string)

>>> <class 'str'>

json_string_2 = json.dumps(data, indent=4)

print(json_string_2)

>>> {

    "name": "Guido van Rossum",

    "age": 65,

    "degree": [

        "mathematics",

        "computer science"

    ],

    "retired": true,

    "carrer": {

        "google": {

            "from": 1999,

            "to": 2013

        },

        "dropbox": {

            "from": 2013,

            "to": 2019

        }

    }

}Code language: JavaScript (javascript)

Dealing with objects that the json module does not support

The json module supports the majority of built-in types of data, but there are a few sorts of data that it does not, complicated being one of them. Similarly, because it lacks information about user-defined types of data, the json module cannot self-serialize or deserialize them. If you proceed normally, you will encounter an error.

z = 3 + 2j

print(type(z))

>>> <class 'complex'>

json.dumps(z)

>>> TypeError: Object of type complex is not JSON serializableCode language: HTML, XML (xml)

With this type of data, you should provide additional help functions to Python, such as default for serialize and object hook for deserialize.

Serialize complex data types, for example.

z = 3 + 2j

def encode_complex(z):

    if isinstance(z, complex):

        return (z.real, z.imag)

    else:

        raise TypeError("This type is not Json serializable")

 

a = json.dumps(z, default=encode_complex)

print(a)

>>> [3.0, 2.0]Code language: PHP (php)

Deserialize complex data type

def decode_complex(dct):

    return complex(dct['real'], dct['imag'])

a = """ {

"real": 3.0,

"imag": 2.0 }

"""

b = json.loads(a, object_hook=decode_complex)

print(b)

>>> (3 + 2j)

print(type(b))

>>> <class 'complex'>Code language: PHP (php)

Serialize and deserialize the user-defined class

You can use dict attributes to serialize and desialize data with self-defined classes. The object’s properties are stored in a dictionary returned by dict.

class People(object):

    def __init__(self, first_name: str, second_name: str):

        self.first_name = first_name

        self.second_name = second_name

people = People("Guido", "Russom")

print(people.__dict__)

>>> {'first_name': 'Guido', 'second_name': 'Russom'}

encode_data = json.dumps(people.__dict__)

print(encode_data)

>>> {"first_name": "Guido", "second_name": "Russom"}

decode_data = People(**json.loads(encode_data))

print(decode_data)

>>> <__main__.People object at 0x7f85a03e1990>

print(decode_data.first_name)

>>> Guido

print(decode_data.second_name)

>>> RussomCode language: HTML, XML (xml)

The ** in decode data = People(**json.loads(encode data)) is a specific python syntax for passing a list of keyword arguments to a function. As a result, we’ve discovered the general approach to working with the json format in Python. I hope you found the information above to be useful.

Conclusion

In this article, we took a look at how to work with JSON Data in Python. We learned with the help of examples how to serialize and deserialize JSON data. As always, If you have found this article useful do not forget to share it and leave a comment if you have any questions. Happy Coding 🙂

Comments

Leave a Reply