Parsing Data from JSON into Python

JSON or Javascript Object Notation is a syntax used for storing and exchanging data over a network. It is inspired by the Javascript programming language used basically in front-end web development, but later it becomes independent and available for almost every programming language, including python. Python has a built-in package named JSON, which can easily work with JSON data.

This tutorial will use the JSON module to perform many operations with data such as parsing, dumping, etc. To work with this tutorial, it is recommended to have the latest python version installed in your OS. It is also recommended to have an IDE installed for writing the code. I will use the open-source IDE Visual Studio Code to write the code; you can use any IDE with which you are familiar.

Importance of JSON

JSON is one of the most important technology for the modern web. Many websites use it for transferring its data to the users and also to other software applications. Earlier, these tasks are performed by languages like XML, but nowadays, it is done using javascript. JSON is independent of programming languages to use. It in many languages like Java, Python, javascript, etc., without much difficulty. It is human-readable, and it is also easy to parse by machines making it one of the reasons commonly used in Web APIs.

Most of the APIs (Application Programming Interfaces) we have today use the JSON for transferring data from the server to other applications like a mobile app, desktop app, and also to its users. So anyone can use the data with their favorite programming language without much hassle. Good knowledge of it be helpful if we want to build an application that uses APIs to perform operations.

JSON is also used to write and store configuration files for many applications. For example, npm uses the package.json file to store the app’s configuration and the packages required. There are also many other use cases, making it one of the most popular files for developers.

JSON syntax

The syntax is straightforward, making it easy to be read by humans and parse and generated by computers. The syntax is inherited by the javascript language used in web development, mainly in the front end. Curly brackets are used to hold objects, square brackets are used to store arrays like javascript, and the data are stored in key: value pairs like in python dictionary. The keys should be of the string data type, and the values can be any of the following data types:

  • String
  • Number
  • JSON Object
  • array
  • Boolean
  • Null

Let us look at a simple example of JSON data syntax.

 {
   "company": "XYZ private limited",
   "location": "India",
   "deals_with": ["Android App Development", "Web development"],
   "head":{  
              "name": "Amit",
              "age": 39 
          },
   
   "employee": [

      {
         "id": "01",
         "name": "David",
         "age": 27
      },

      {
         "id": "04",
         "name": "Shane",
         "age": 33
      }
   ]
}

The above example tells us many things about the JSON syntax. The first thing to note in the above data is that all the data are stored in a parent curly bracket. Inside the curly bracket, there are key: value pairs like we have in the python dictionary. The first two pairs of the data hold string values while the third pair hold an array as the value. The fourth line with key “head” store an object and the fifth key “employee” hold an array. We can also see that we need to give a comma at the end of each key: value pairs except the last one we do in a python dictionary.

Python JSON Module

Python comes with a built-in package named JSON, which can be used while working with the corresponding data. To use the module first, we need to import it in our code as I do in the following code.

import json

The JSON module is a great package, and most of our tasks can be done using it. Now let us see how we can use the JSON module to work with the JSON data.

Parsing JSON

Parsing is the process of converting JSON into a python object. We have two functions in the library of python to perform this operation. The first one is the loads() function and the second one is the load() function.

loads() method

The loads() method (means load from a string) is used to load data from a string and then convert it into a python dictionary. Look at the example below for an illustration.

import json

# storing json data into a multiline string
data_JSON = """
{ 
"id": "01",
"name": "David", 
"age": 27
}
"""

# parsing the json data from string
# converting it into python dictionary 
data_dict = json.loads(data_JSON)

# Displaying the data from the dictionary
print("[+] ID :", data_dict["id"])
print("[+] Name :", data_dict["name"])
print("[+] Age :", data_dict["age"])

In the above code, we first imported the JSON module to use the loads() function. Then we created a multiline string and store some data in it. Then we use the loads() function to convert the string into a python dictionary. After parsing the JSON data into a python dictionary, we displayed it using the print() function.

On running the above code in a python IDE, we will get the following output.

python json loads() function
python JSON loads() function

load() method

In the JSON library, we also have a load() function used to load data from a file and convert it into a python dictionary. It’s easy to confuse the load() and the loads() function and treat them the same, but there is a difference. The load() function loads the data from a file while the loads() load data from a string, the s in the ending of loads() function refers to a string. Now let us see a practical demo of the load() function. First, create a JSON file and name it data.json, and put the following data on it.

{
"company": "XYZ private limited",
"location": "India",
"deals_with": ["Android App Development", "Web development"],
"head": { "name": "Amit", "age": 39 },
"employee": [
{ "id": "01", "name": "David", "age": 27 },
{ "id": "04", "name": "Shane", "age": 33 }
]
}

After creating the JSON file, we can use the json.load() method to parse the JSON of the file and convert it to the python dictionary. Look at the below code for illustration.

# importing the json module
import json

# opening the file data.json using python open() method
file_obj = open(file="data.json")

# Using the load() function to load json from the file object
json_dict = json.load(file_obj)

# displaying the json data
print("[+] Company Name:", json_dict["company"])
print("[+] Company Deals With:", json_dict["deals_with"])
print("[+] Company Location:", json_dict["location"])
print("[+] Employee Details:", json_dict["employee"])

In the above code, we first imported the JSON module into our code using the import statement. Then we open the data.json file, which we created earlier using the python open() function. Next, we pass the file object to the load() function as an argument that will parse and convert the file’s data into a python dictionary. We display the data in the python dictionary by accessing them using dictionary keys.

On running the above code using a python interpreter, we will get the following output.

python json load() function
python json load() function

Dumping JSON

Dumping means converting a python object into a JSON object. Like parsing, dumping can also be done in python using two functions. The first one uses the dumps() function and the second one is using the dump() function.

We can convert the following type of python objects into their respective JSON data type.

PYTHON JSON
dictionary object
list array
tuple array
int number
str string
True true
False false
None null

After using the dump method to convert Python objects to JSON, we can transfer them over networks for which JSON syntax is mainly used.

dumps() method

The dumps() function is used to dump a python object into a JSON string. Look at the below example for a practical demo of dumping python objects.

import json

# dumping a python list into json array 
print(json.dumps([1, 2, 3, 4]))

# dumping a python integer into json number 
print(json.dumps(1))

# dumping a python string into json string 
print(json.dumps("CodeUnderscored"))

# dumping a python tuple into json array 
print(json.dumps(('python', 'json', 'api')))

# dumping a python True into json 
print(json.dumps(True))

# dumping a python None into json 
print(json.dumps(None))

# dumping a python dictionary into json object 
print(json.dumps(
{
'name': 'codeunderscored',
'address': 'https://thirdeyemedia.wpmudev.host/codeunderscored'
}
))

In the above code, we first imported the JSON module, and then we use its dumps() function to dump some of the common python data types into their respective data types.

Output:

python json dumps function
python json dumps function

In the above output, we can see that the boolean value True has been changed into true, and None has been changed into null.

Pretty Printing the JSON Data

In the previous code, we have displayed some objects by changing them from python to JSON. But the output looks something dull and not much indented. To print the data into a pretty format, we can use the indent argument with the dumps() function and set it to 4. Look at the below code for illustration.

import json

# dumping a python tuple into json array
print(json.dumps(('python', 'json', 'api'), indent=4))

# dumping a python dictionary into json object
print(json.dumps(
{
'name': 'codeunderscored',
'address': 'https://codeunderscored.com'
}, indent=4
))

Output:

python json preety printing
python json pretty-printing

using dump() method

We also have a dump() function in the JSON library. It works the same as the dumps() function, but instead of a string, it will output the data into a file. It accepts a python data object and a file object as its argument and writes the data object in JSON format into the file whose file object is provided. We can also use the indent argument with it for pretty printing in the file. Take a look at the below code for illustration.

import json

file_obj = open('data.json', 'w')

# dumping a python dictionary into json object
json.dump(
{
'name': 'codeunderscored',
'address': 'https://codeunderscored.com'
}, file_obj, indent=4
)

We first imported the json module, which is required to use the dump() function. Next, we open a file named data.json using the open() function in reading and writing mode. Then we pass the file object with the python object as an argument to the dump() function. On running the code, we will have a JSON file created with the name data.json containing the above code’s JSON data.

Conclusion

In this tutorial, we have learned how to use the JSON module of python to parse and dump JSON data. The things that we learned in this tutorial are good enough for tasks like writing configurations files or working with APIs. I hope you found the resource helpful. If yes, we would appreciate it if you can share the article with your friends using the social icons below.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *