JavaScript Object Notation aka JSON is a very lightweight standard data representation format. It is used for storing and transporting data. It was derived from Javascript but is now language-independent. It was created with the intention of making a text-based interchange format that is easily readable by humans.

JSON is compatible with most programming languages and can easily be integrated as they have built-in functionalities to read, write and parse JSON.

JSON is commonly used across the computing world for APIs and for Config files of different programs such as games. It is also used to transfer data from a server to the client and vice versa.

In this post, we will go through a guide on how to read, write and parse JSON in python. Python has a built-in package named JSON which can be used to manipulate JSON data.

How to Convert JSON to Python Dictionary (Parse JSON)

We can use the json.loads() method to parse JSON into a Python dictionary. We have to provide the JSON in string format to the json.loads() function because this function takes the string and converts the string into a Python dictionary.

In the example given below, the employee is a JSON string while the employee-dict is a python dictionary.

import json

employee = ‘{“First_Name”: “John”, “Second_Name”: “Doe”, “id”: “01”, “Department”: “Health”}’

employee_dict = json.loads(employee)

print(employee_dict)

Output

How to Read, Write & Parse JSON in Python JSON Programming Python

How to Convert a Python Dictionary to a JSON String

In the previous section, we learned how to convert a JSON string to a Python dictionary. Now we will do the opposite and convert the Python dictionary into a JSON string.

import json

employee_dict = {‘First_Name’: ‘John’, ‘Second_Name’: ‘Doe’, ‘id’: ’01’, ‘Department’: ‘Health’}

employee = json.dumps(employee_dict)

print(employee)

Output

How to Read, Write & Parse JSON in Python JSON Programming Python

How to Read a JSON File

We can use the json.load() method to read a JSON object, in Python.

First, created a data.json file with following content:

cat /home/user/data.json 

{"First_Name": "John", "Second_Name": "Doe", "id": "01", "Department": "Health"}

Then run the below program to data from JSON file and print on the screen.

import json

with open(‘/home/rahul/data.json’) as f:

  employee_data= json.load(f)

print(employee_data)

Output

How to Read, Write & Parse JSON in Python JSON Programming Python

How to Write JSON to a File using Python

We can use the json.dump() method to write JSON to a .json file.

The program given below creates a new file named employee.json. If the file already exists then it only opens the file in ‘w’ mode. The ‘w’ mode means that the file is opened in write mode.

Then json.dump() converts the python dictionary “employee_dict” into a JSON string and writes it into the json file.

import json

employee_dict = {“First_Name”: “John”, “Second_Name”: “Doe”, “id”: “01”, “Department”: “Health”}

with open(’employee.json’, ‘w’) as json_file:

  json.dump(employee_dict, json_file)

Output:

How to Read, Write & Parse JSON in Python JSON Programming Python

Conclusion

JSON has become the most common method to store and transfer data in recent times. Its ease of use has made it wildly popular among developers.

In this write-up we have learned json.loads(), json.dumps(), json.load(), and json.dump() methods. These methods help us manipulate, read, write and parse JSON in python.