How to Parse JavaScript Object Notation (JSON) in python

How to Parse JavaScript Object Notation (JSON) in Python

The JSON format was inspired by the object and array format used in the JavaScript language.

But since Python was invented before JavaScript, Python’s syntax for dictionaries and lists influenced the syntax of JavaScript Object Notation (JSON).

So the format of JavaScript Object Notationis nearly identical to a combination of Python list and dictionary data structures.

Video Tutorial – How to Parse JavaScript Object Notation (JSON) in python

Example simple JavaScript Object Notation (JSON)

{ 
    "id" : "001",
    "x" : "2",
    "name" : "Mahesh"
}

In the above example, JSON has three elements of the for key and value pairs. The id, x, and name are keys, and 001, 2, and Mahesh are values.

Another example JavaScript Object Notation (JSON)

{
    "name" : "Mahesh",
    "id" : "001",
    "x" : "2",
    "phone" : {
        "type" : "mobile",
        "number" : "+91 7411043272"
    },
    "email" : {
        "hide" : "yes"
    }
}

Here the first three elements are the same as the previous example. The fourth and fifth elements have values as dictionaries. The phone elements dictionary has two elements type and number, similarly, email has to hide as the element.

See also  Structured Query Language with a python program

Parsing JavaScript Object Notation (JSON) Document

To parse and extract values from JSON, first, we need to import the JSON library. Then the loads() function is used to load JSON into a list variable. The elements of JSON were extracted using the list variable with keys as the index.

The following example shows how to extract elements of simple JSON using JSON library of python.

import json
data = '''
[
    { 
        "id" : "001",
        "x" : "2",
        "name" : "Mahesh"
    } ,
    { 
        "id" : "009",
        "x" : "7",
        "name" : "Rahul"
    }
]'''
    
info = json.loads(data)
print('User count:', len(info))
print ('------------------')
print('User Details')
for item in info:
    #print (item)
    print ('..................')
    print('Name:', item['name'])
    print('Id:', item['id'])
    print('X:', item['x'])

Output:

User count: 2
------------------
User Details
..................
Name: Mahesh
Id: 001
X: 2
..................
Name: Rahul
Id: 009
X: 7

The following example shows how to extract elements of complex JSON using JSON library of python.

import json
data = '''
[
    { 
        "id" : "001",
        "x" : "2",
        "name" : "Mahesh",
        "phone" : {
            "type" : "mobile",
            "number" : {
                "xyz": "4321"
            }
        }
    } ,
    { 
        "id" : "009",
        "x" : "7",
        "name" : "Rahul",
        "phone" : {
            "type" : "mobile",
            "number" : {
                "xyz": "1234"
            }
        }
    }
]'''
    
info = json.loads(data)
print('User count:', len(info))
print ('------------------')
print('User Details')
for item in info:
    #print (item)
    print ('..................')
    print('Name:', item['name'])
    print('Id:', item['id'])
    print('X:', item['x'])
    print('Type:', item['phone']['type'])
    print('Number --> xyz:', item['phone']['number']['xyz'])

Output:

User count: 2
------------------
User Details
..................
Name: Mahesh
Id: 001
X: 2
Type: mobile
Number --> xyz: 4321
..................
Name: Rahul
Id: 009
X: 7
Type: mobile
Number --> xyz: 1234

Summary:

This tutorial discusses how to Parse JavaScript Object Notation (JSON) in Python. If you like the tutorial do share it with your friends and subscribe to the YouTube Channel for more videos and tutorials.

See also  Python for Beginners Video Tutorial

Leave a Comment

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