The Simplest JSON Python Module Tutorial: Parsing JSON With Python

JSON is a very common format for storing information. If you have ever used an API like Flickr App garden, or the Google Could Storage, you’ll know how common JSON is on the web.

Introduction

json-python-tutorial

In this tutorial, I’ll explain how you can parse JSON data using the Python’s built-in JSON Module.

JSON Example

The first thing you need to do is import the Python JSON module. You can do that by adding import json to the top of your code. You’ll also need to store your json data in a string. For that, you can simply create a python multi-line string. Here’s how your code should look like after doing that.

import json

user_info = ''' {
    "users": [
        {
            "id": "cde9e392-8cd2-404c-8800-3c9286c812f8",
            "name": "Ernest K. Gonzalez"
        },
        {
            "id": "15c3d4f9-7abd-4aec-8a44-e16631bdec53",
            "name": "Robert B Carlson"
        } 
     ],
    "employees": [
        {
            "id": "4a235940-a772-4890-91ca-062bfdac0b62",
            "name": "Josef L. Cleek"
        },
        {
            "id": "00189f27-7631-43c0-81c2-1990bf4c7483",
            "name": "Melinda G. Flores"
        }
    ]
} '''

Converting String to JSON Data

Although the string user_info perfectly resembles what json should look like, Python doesn’t know that.  To convert and save it into a python object, we need to use the json.loads() method.

How JSON data is stored

Internally, the key and value pairs that you store in in JSON are encoded by Python at runtime. The JSON object is converted to a Python dict and a JSON String is converted to a Python str. Here’s how each data type corresponds in Python.

JSON Python
object dict
array list
string str
number (int) int
number (real) float
true True
false False
null None

There are other data types like Infinity, -Infinity, and NaN that have no corresponding Python value in JSON.

When you convert the JSON to a python object using json.loads(), the return value you get is a data type corresponding to the values you have in your JSON.

You can store your JSON –> Python data by storing the return value of json.loads() as follows.

data = json.loads(user_info)

Accessing elements

Once your data is stored in a variable, you can access it using various methods.

If you use print(data) at this time to print the dictionary that we have here, you will see an output like this:

{
    "users": [
        {
            "id": "cde9e392-8cd2-404c-8800-3c9286c812f8",
            "name": "Ernest K. Gonzalez"
        },
        {
            "id": "15c3d4f9-7abd-4aec-8a44-e16631bdec53",
            "name": "Robert B Carlson"
        } 
     ],
    "employees": [
        {
            "id": "4a235940-a772-4890-91ca-062bfdac0b62",
            "name": "Josef L. Cleek"
        },
        {
            "id": "00189f27-7631-43c0-81c2-1990bf4c7483",
            "name": "Melinda G. Flores"
        }
    ]
}

From here, you can do anything with this data. Iterating or accessing individual elements and programmatically processing this data is now possible.

Accessing individual elements

You can print individual elements in this dictionary as follows:

print(data['users'][0]['name]) # --> Earnst K. Gonzalez 

OR

print(data.get('users')[0].get('name')) # --> Earnst K. Gonzalez

Both will give you the same result.

Accessing multiple elements via loops

You can use loops for accessing all the elements at once. For e.g., if you want to access all the names in the dictionary, you can use the following code:

for person in data["users"]:
    print(person["name"]) 
''' Output --> Ernest K. Gonzalez 
Robert B Carlson '''

Using these methods, you can access all the values anywhere in the dictionary.

Modifying JSON

Parsing JSON data is good. But, what if you want to modify the JSON data you already have with you?

Deleting a key from the dictionary

You can use Python’s keyword del to delete any value in the dictionary.

del data['users'][0]['name'] #--> delete "Ernest K. Gonzalez" from the dictionary

Deleting multiple keys

You can use a for loop to delete multiple values.

for person in data["users"]:
    del person["name"]

After this loop, Python will delete the values from the dictionary. But, you will need to store new data somewhere. I’ll store it in a variable named new_users_string.

new_users_string = json.dumps(data)

To see what the string data looks like now, you can print the new_users_string.

print(new_users_string)

The output you will get will look something like the following:

{"users": [{"id": "cde9e392-8cd2-404c-8800-3c9286c812f8"}, {"id": "15c3d4f9-7abd-4aec-8a44-e16631bdec53"}], "employees": [{"id": "4a235940-a772-4890-91ca-062bfdac0b62", "name": "Josef L. Cleek"}, {"id": "00189f27-7631-43c0-81c2-1990bf4c7483", "name": "Melinda G. Flores"}]}

Formatting the output

As you can see, the output is very long and hard to read. To fix that, you can use an option called indent=value in the json.dumps() method to make it more readable.

So, instead of writing

new_users_string = json.dumps(data)

You type

new_users_string = json.dumps(data, indent=4)

Now, your output will look more legible.

{
    "users": [
        {
            "id": "cde9e392-8cd2-404c-8800-3c9286c812f8",
        },
        {
            "id": "15c3d4f9-7abd-4aec-8a44-e16631bdec53",
        } 
     ],
    "employees": [
        {
            "id": "4a235940-a772-4890-91ca-062bfdac0b62",
            "name": "Josef L. Cleek"
        },
        {
            "id": "00189f27-7631-43c0-81c2-1990bf4c7483",
            "name": "Melinda G. Flores"
        }
    ]
}

That’s it. If you have any questions about anything in this post, please use the comments section to let me know.

Leave a Reply

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