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 |
list | array |
dictionary | object |
unicode character | string |
True | True |
False | False |
None | Null |
int or long number | int number |
float number | real number |
Let’s have a look at how to perform the encoding process in Python.
Subscribe to our Newsletter
Join our editors every weekday evening as they steer you through the most significant news of the day, introduce you to fresh perspectives, and provide unexpected moments of joy
Your newsletter subscriptions are subject to AIM Privacy Policy and Terms and Conditions.
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:
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.
#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:
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.
- default(obj) – This method is implemented in a subclass. It returns a serializable object for the specified argument.
- encode(obj) – It performs the encoding process, i.e. Python object to JSON-formatted string conversion, as done by the dumps() method.
- 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:
- 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.
- decode(obj) – It decodes the JSON object ‘obj’ as done by the loads() method.
- 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