Python

Python JSON Parsing

Python JSON Parsing

In this article, we will discuss how to parse JSON data in python programming language. We will go through several examples to understand the JSON parsing mechanism in python.

What is JSON?

JSON is an acronym that stands for JavaScript Object Notation. JSON is a lightweight text-based representation for organizing and storing structured data. JSON data is represented as ordered lists and key-value pairs. It is much easier and cleaner to represent the structured data in JSON than XML.

Sample JSON Object

Here is an example of JSON file –

{

   “Name”: ”Elie”,

   “Student ID”: “27389,

    “Phone”: “82677,

    “Department”: “Finance”

 }

The JSON object is represented within the curly braces. A JSON object can contain multiple JSON data. Each JSON data is represented by a comma. JSON data is nothing but a {Key:Value} pair.

Parsing Libraries in Python

There is a built-in library for encoding and decoding JSON data in python script. To use the JSON library, we have to import the JSON library as shown below –

In this article, we will focus on the json library for parsing JSON data.

Example-1

In this example, we first import the “json” library. This is the built-in package in Python. We have declared a JSON string called “json_data”. The JSON string can be parsed using the json.loads() method. This method would return a Python dictionary. The dictionary elements are then printed as shown in the below output image.

import json


json_data =  '{ "student_name":"Elie", "roll":20, "Class":11, "sub1":"Math","sub2":"Phy","sub3":"Chem","city":"Chicago"}'


# Parse JSON data using loads() function

parsed_data = json.loads(json_data)


# Print the result

print(parsed_data["student_name"])

print(parsed_data["roll"])

print(parsed_data["city"])

Example-2

In this example, we will see how to parse the Python dictionary and convert it to a JSON string. We have first declared a python dictionary called “py_obj”. The python dictionary is then converted to JSON string by using the json.dumps() method. We have then printed the JSON object as shown in the below output image.

import json


# declare a python dictionary

py_obj = {

  "student_name": "Elie",

  "roll": 20,

  "Class":11,

  "sub1":"Math",

  "sub2":"Phy",

  "sub3":"Chem",

  "city": "Chicago"

}


# convert the python dictionary into JSON:

jsob_obj = json.dumps(py_obj)


# Print the JSON object

print(jsob_obj)

Example-3

This example shows how to read a JSON file and parse the JSON data content of the file. Here is the input file that is being considered for this example –

{ "student_name":"Elie", "roll":20, "Class":11,

"sub1":"Math","sub2":"Phy","sub3":"Chem","city":"Chicago"}

We first import the “json” library and then read the “json_input.json” file and parse it’s content using the json.load() method and stored in “json_data” variable. We have then printed the “json_data” variable as shown in the below output screenshot.

import json


with open('json_input.json') as f:

  json_data = json.load(f)


# print output

print(json_data)

Example-4

This example shows how to convert a Python dictionary into JSON string and write it to a file.

First, the “json” package has been imported. A Python dictionary “py_obj” is declared. The python dictionary i.e. “py_obj” is then converted to a JSON string using json.dump() method. This JSON string is then written to a file. If the output file does not exist, the file will be created and the JSON string will be written.

import json


# declare a python dictionary

py_obj = {

  "student_name": "Elie",

  "roll": 20,

  "Class":11,

  "sub1":"Math",

  "sub2":"Phy",

  "sub3":"Chem",

  "city": "Chicago"

}


with open('json_output.txt', 'w') as json_output_file:

  json.dump(py_obj, json_output_file)

Example-5

This is the example to show how to print JSON string in user friendly manner. In this example, we have used the indent as 3 and the keys will be sorted in ascending order, while printing the JSON string.

import json



# declare a python dictionary

py_obj_1 = '{"student_name": "Elie","roll": 20,"Class":11,"sub1":"Math","sub2":"Phy","sub3":"Chem","city": "Chicago"}'



py_obj_2 = json.loads(py_obj_1)



# Print JSON string

print(json.dumps(py_obj_2, indent = 3, sort_keys=True))

Conclusion

In this article, we have briefly discussed about the JSON file format and how to parse JSON file/data in Python. In this article, we have covered the “json” library and seen several examples to parse JSON data.

About the author

Sukumar Paul

I am a passionate software engineer and blogger. I have done my Masters in Software Engineering from BITS PILANI University, India. I have very good experience in real-time software development and testing using C, C++, and Python. Follow me at thecloudstrap.com.