Now Reading
How To Handle JSON Data using Python?

How To Handle JSON Data using Python?

JSON

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. 

Python JSON 
listarray
dictionaryobject
unicode characterstring
TrueTrue
FalseFalse
NoneNull
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

print(str)

Output:

JSON output 1

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

type(dictionary)

Output: dict

type(string)

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

print(python_obj)

Output:

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

Check the data types of json_data and python_obj

type(json_data)

Output: str

type(python_obj)

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.

See Also
OpenCV cover image

 #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
 print(read_data) 

Output:

{'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
 json_encode 

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

Check the data type of json_encode object

type(json_encode)

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
   json_decode 

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

Check the data type of json_decode object

type(json_decode)

Output: dict

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

Join Our Telegram Group. Be part of an engaging online community. Join Here.

Subscribe to our Newsletter

Get the latest updates and relevant offers by sharing your email.

Copyright Analytics India Magazine Pvt Ltd

Scroll To Top