Python JSON – Guide

JSON stands for JavaScript Object Notation. JSON is a lightweight data format used for data interchange between multiple different languages. It is easy to read for humans and easily parsed by machines.

CONTENTS

  1. Brief Overview
  2. Purpose of JSON
  3. Serializing JSON
    • dumps() function
    • dump() function
  4. Deserializing JSON
    • loads() function
    • load() function
  5. Complex number encoding
  6. Complex JSON object decoding

1. Brief Overview

JSON stands for JavaScript Object Notation. JSON is a lightweight data format used for data interchange between multiple different languages. It is easy to read for humans and easily parsed by machines.

It’s pretty common for websites to return JSON from API’s so that the information is easy to parse by different programming languages.

In Python, the text of JSON is read as quoted-string which contains the value in key-value mapping within { }. Once parsed, it is available as a dictionary object in Python.

Python comes with a built-in package called json for encoding and decoding JSON data.

For working with json type files, first you need to import the json library.

import json

2. Purpose of JSON

The process of converting python to JSON is done by serialization.

The term ‘serialization’ refers to the transformation of data into a series of bytes (hence serial) to be stored.

Since the JSON is read by other languages, various Python-specific objects are converted to a standard JSON acceptable format. For example, a list and a tuple, which are python specific, are converted to arrays when storing it as JSON format.

Likewise, a JSON object is imported and parsed as a python dict.

But, why store it as a ‘dict’ specifically?

Because, the python ‘dict’ is very similar in structure to a JSON, as it can hold other types of data structures within, such as list, tuples, and even other dicts etc.

Below image shows how the various datatype in Python are stored when converted to JSON.

3. How to convert Python Dict to JSON?

So, how to convert a Python dict to JSON?

json.dump() and json.dumps() function can be used to accomplish this. The difference between the two is, ‘json.dumps()’ converts a ‘dict’ to a string format whereas ‘json.dump()’ can be used to store it to a JSON file on disk storage.

You have to pass the dictionary as a necessary argument for both functions.

First let’s look into dumps() function.

Python objects are converted to JSON by following this conversion.

dumps() function:

This function is used to convert a Python object into a JSON string. Remember, the s in dumps() stands for the string. Easy to remember.

First I am going to create a python dictionary called data. Then, convert this dictionary to JSON format using dumps() function.

# Convert dict to json string
data = {"Name":"Alex","Roll":"25","Score":"94", "Hometown":"Mumbai"}
json_object = json.dumps(data) 
print(json_object)
{"Name": "Alex", "Roll": "25", "Score": "94", "Hometown": "Mumbai"}

The resulting json_object is a ‘string’ type object.

type(json_object)
str

For indenting the data, you can use indents= command inside the dumps function.

# Indent by 4 spaces
json_object = json.dumps(data, indent = 4) 
print(json_object)
{
    "Name": "Alex",
    "Roll": "25",
    "Score": "94",
    "Hometown": "Mumbai"
}

You can now see that the file is indented properly.

There is also an another built-in command named `sort_keys’ which will sort the keys in the dictionary alphabetically.

# Sort the Keys in JSON
json_object = json.dumps(data, indent = 4,sort_keys=True) 
print(json_object)
{
    "Hometown": "Mumbai",
    "Name": "Alex",
    "Roll": "25",
    "Score": "94"
}

As you can see above, now the keys are arranged alphabetically.

There is also another argument which you can use called separators . You can change the separator to any alternative in this (“, ” , “: ” , “,” , “:”).

dump() function

Json.dump() will transform the dictionary to a JSON string and it will be saved in a file as well.

Using Python’s context manager, let’s create a file called dump.json, open it in writing mode and dump the dictionary onto the file.

with open('dump.json','w') as d:
  json.dump(data,d)

If you do this then a file named dump.json will be downloaded in the directory in which you opened the python file containing the JSON string of our dictionary.

Similar to dumps() function, you can use indent and sort_keys command in dump() function also.

4. Deserializing JSON:

Deserialization means converting a JSON object into their respective python object.

The functions used to do this are load() and loads().

load() function:

json.load() is used to convert a JSON object to a Python dictionary. It takes a JSON file object as an argument, parses the JSON data, converts it into a Python dictionary with the data, and returns it back to you.

I am going to use an existing json file containing a list of 5 users and their information.

You can download the dataset from this link –https://www.learningcontainer.com/wp-content/uploads/2019/10/Sample-JSON-file-with-multiple-records-download.json

You are using any JSON file.

import json 
f = open('jsonfile.json',)  
data = json.load(f) 
type(data)
dict

See that the json file has been now converted into a dictionary data type. You can also access the keys and values stored and use it as any other python dictionary.

That’s all there is to it.

for person in data['users']:
  print(person)
f.close()
{'userId': 1, 'firstName': 'Krish', 'lastName': 'Lee', 'phoneNumber': '123456', 'emailAddress': 'krish.lee@learningcontainer.com'}
{'userId': 2, 'firstName': 'racks', 'lastName': 'jacson', 'phoneNumber': '123456', 'emailAddress': 'racks.jacson@learningcontainer.com'}
{'userId': 3, 'firstName': 'denial', 'lastName': 'roast', 'phoneNumber': '33333333', 'emailAddress': 'denial.roast@learningcontainer.com'}
{'userId': 4, 'firstName': 'devid', 'lastName': 'neo', 'phoneNumber': '222222222', 'emailAddress': 'devid.neo@learningcontainer.com'}
{'userId': 5, 'firstName': 'jone', 'lastName': 'mac', 'phoneNumber': '111111111', 'emailAddress': 'jone.mac@learningcontainer.com'}

You can also access each key separately by changing the line to print(person['firstName']).

Similar to dump() and dumps() function, there is also another function similar to this called loads().

loads() function

By using json.loads() the function you can simply convert JSON data into Python data.

But unlike load(), this take does not take a file as input, this takes a string as input.

newdata = """
{
    "Student": {
        "name": "robert",
        "rollno": "86",
        "marks": [
            {
                "maths": "100",
                "science": "96"
            }
        ]
    }
}
"""
data = json.loads(newdata)
type(data)
dict

Like load(), the loads() function also converts it into a python dictionary.

5. Encoding Complex Numbers to JSON

Encoding is defined as converting the text or values into an encrypted form that can only be used by the desired user through decoding it.

Generally, we can’t pass a complex number into JSON, if you try sending it you get an error YTypeError: Object of type 'complex' is not JSON serializable.

complex_obj = json.dumps(9 + 6j)
print(complex_obj)
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-62-71a7bf7fdb26> in <module>()
----> 1 complex_obj = json.dumps(9 + 6j)
      2 print(complex_obj)


/usr/lib/python3.6/json/__init__.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
    229         cls is None and indent is None and separators is None and
    230         default is None and not sort_keys and not kw):
--> 231         return _default_encoder.encode(obj)
    232     if cls is None:
    233         cls = JSONEncoder


/usr/lib/python3.6/json/encoder.py in encode(self, o)
    197         # exceptions aren't as detailed.  The list call should be roughly
    198         # equivalent to the PySequence_Fast that ''.join() would do.
--> 199         chunks = self.iterencode(o, _one_shot=True)
    200         if not isinstance(chunks, (list, tuple)):
    201             chunks = list(chunks)


/usr/lib/python3.6/json/encoder.py in iterencode(self, o, _one_shot)
    255                 self.key_separator, self.item_separator, self.sort_keys,
    256                 self.skipkeys, _one_shot)
--> 257         return _iterencode(o, 0)
    258 
    259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,


/usr/lib/python3.6/json/encoder.py in default(self, o)
    178         """
    179         raise TypeError("Object of type '%s' is not JSON serializable" %
--> 180                         o.__class__.__name__)
    181 
    182     def encode(self, o):


TypeError: Object of type 'complex' is not JSON serializable

But in python you can actually find the real and imaginary part by object.real and object.imag.

You need to create a function which checks whether the value stored in the variable is complex or not.

import json
def complexnum(object):
    if isinstance(object, complex):
        return [object.real, object.imag]
    raise TypeError(repr(object) + " is not JSON serialized")

You can see that I have created the function to check whether it’s complex or not and split it into real and imaginary part.

complex_obj = json.dumps(9 + 6j, default=complexnum)
print(complex_obj)
[9.0, 6.0]

9.0 is the real part and 6.0 is the imaginary part.

6. Complex JSON object decoding in Python

To decode complex object in JSON, use an object_hook parameter which checks JSON string contains the complex object or not.

import json
def complexnum(objct):
    if '__complex__' in objct:
      return complex(objct['real'], objct['img'])
    return objct
complexrep =json.loads('{"__complex__": true, "real": 9, "img": 6}', object_hook = complexnum)
normalrep =json.loads('{"real": 10, "img": 8}', object_hook = complexnum)
print(complexrep)
print(normalrep)
(9+6j)
{'real': 10, 'img': 8}
  1. Requests in Python
  2. 101 Numpy Exercises for Data Analysis
  3. 101 Pandas Exercises for Data Analysis

This article was contributed by Venmani.