Active Hackathon

How To Handle JSON Data using Python?

This article talks about how to deal with JSON (JavaScript Object Notation) formatted data using Python codes.

JSON (JavaScript Object Notation) is a simple language-independent and human-readable text format derived from JavaScript. It enables easy storage and exchange of structured data comprising attribute-value pairs and arrays or any such serializable data. This article talks about how to deal with JSON formatted data using Python codes. 

Encoding (Python to JSON)

The encoding process refers to the conversion of Python to JSON objects. Python provides a JSON package having API to operate on JSON data. Following is a list of Python objects and corresponding JSON objects they are encoded to. 


Sign up for your weekly dose of what's up in emerging technology.
Python JSON 
unicode characterstring
int or long numberint number
float numberreal number

Let’s have a look at how to perform the encoding process in Python.

Note: The code snippets throughout this article have been implemented using Google colab with Python 3.7.10 and json 2.0.9 versions.

Import json library

Import json as js

Create a dictionary having data of various types.

 dictionary = {
   "name": "Alex",
   "gender": "Male",
   "age" : 38,
   "Indian": True,
   "brothers": ("Ron","Peter"),
   "sisters": ['Elizabeth'],
   "house": [
     {"city": "LA", "price": 2380.5},
     {"city": "NYC", "price": 1120}

Convert the dictionary into JSON-formatted string

string = json.dumps(dict, indent=4, sort_keys=True)

Print the converted object



JSON output 1

Check the type of ‘dictionary’ and ‘string’ objects.


Output: dict


Output: str

Decoding (JSON to Python)

The process of converting JSON objects back to Python objects is referred to as decoding. The list of which python objects get converted to which JSON object type has been provided in the ‘Encoding’ section above. The reverse conversion of those objects occurs in the process of decoding. 

Create a JSON-formatted String object

json_data = '{  "car":  { "company":  "Hyundai",  "name":  "EON",  "number":  2889}}'

Convert it into Python object using loads() method

python_obj = js.loads(json_data)

Display the Python object



{'car': {'company': 'Hyundai', 'name': 'EON', 'number': 2889}}

Check the data types of json_data and python_obj


Output: str


Output: dict 

Writing to a JSON file

Consider the ‘dictionary’ named Python object created in the ‘Encoding’ section above. 

Write the dictionary’s data to a JSON file using dump() method.

 #Open the JSON file (create if it does not exist) in write mode
 with open("myfile.json", "w") as wfile:
     js.dump(dictionary, wfile) 

myfile.json now looks like:

JSON output 2

Reading a JSON file

The above created JSON file myfile.json can be read using load() function

 #Open the file to be read
 with open('myfile.json') as fobj:
         # store the read data in JSON object
         read_data = js.load(fobj)
 #Display the read data


{'name': 'Alex', 'gender': 'Male', 'age': 38, 'Indian': True, 'brothers': ['Ron', 'Peter'], 'sisters': ['Elizabeth'], 'house': [{'city': 'LA', 'price': 2380.5}, {'city': 'NYC', 'price': 1120}]}

JSONEncoder class

JSONEncoder class provides the following three methods to serialize data during the encoding process.

  1. default(obj) – This method is implemented in a subclass. It returns a serializable object for the specified argument. 
  2. encode(obj) – It performs the encoding process, i.e. Python object to JSON-formatted string conversion, as done by the dumps() method.
  3. iterencode(obj) – It encodes the ‘obj’ object and returns its string-converted representation as it becomes available.

Have a look at an example of encoding a Python dictionary using encode() method.

 #Import the JSONEncoder class
 from json.encoder import JSONEncoder
 #Create a Python dictionary
 shape_dictionary = { "shape": ["circle", "square", "triangle" ]}
 # Encode the dictionary
 json_encode = JSONEncoder().encode(shape_dictionary)
 #Display the encoded output

Output: {"shape": ["circle", "square", "triangle"]}

Check the data type of json_encode object


Output: str

JSONDecoder class

JSONDecoder is a class for performing deserialization using the following three methods:

  1. default(str) – It is implemented in the subclass and decodes ‘str’ (a String instance comprising a JSON document). If the document is invalid, it raises JSONDecodeError
  2. decode(obj) –  It decodes the JSON object ‘obj’ as done by the loads() method.
  3. raw_decode(obj) – It decodes the String object ‘str’ starting with a JSON document. It returns a tuple comprising Python-converted form of the JSON document and an index in the ‘str’ marking end of the JSON document.

   Here’s how we can decode a JSON string using decode() method.

    #Import the JSONDecoder class
    from json.decoder import JSONDecoder
   #Create a JSON-formatted string
    colour_string = '{ "colour": ["red", "yellow"]}'
    # Decode the string
    json_decode = JSONDecoder().decode(colour_string)
  #Display the decoded output

 Output: {'shape': ['circle', 'square', 'rectangle']}

Check the data type of json_decode object


Output: dict

  • Google colab notebook of the above code snippets is available here.
  • Refer to the official JSON package’s documentation here.

More Great AIM Stories

Nikita Shiledarbaxi
A zealous learner aspiring to advance in the domain of AI/ML. Eager to grasp emerging techniques to get insights from data and hence explore realistic Data Science applications as well.

Our Upcoming Events

Conference, in-person (Bangalore)
Machine Learning Developers Summit (MLDS) 2023
19-20th Jan, 2023

Conference, in-person (Bangalore)
Rising 2023 | Women in Tech Conference
16-17th Mar, 2023

Conference, in-person (Bangalore)
Data Engineering Summit (DES) 2023
27-28th Apr, 2023

Conference, in-person (Bangalore)
MachineCon 2023
23rd Jun, 2023

3 Ways to Join our Community

Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

Telegram Channel

Discover special offers, top stories, upcoming events, and more.

Subscribe to our newsletter

Get the latest updates from AIM