Menu Close

How to Convert JSON To Python Object

How to convert json to python object

In this article, you will learn all about how to convert JSON to Python Object. To perform this operation we are going to use the Python JSON module which is a pre-defined module.
Python JSON module provides a function to convert JSON data to Python dictionary. In the previous tutorial, we have seen all about convert Python Dictionary to JSON data using the Python JSOM module.

Before going through this article, let’s see a little about Python Dictionary and JSON.

Python Dictionary

Python dictionary is the data type that stores the items in the form of key-value pair. Dictionary in Python does not support the duplicate value, Python dictionary checks the duplicate data on the basis of the key. Dictionary values can be accessed with the help of their corresponding value.

Let’s see an example to understand Python Dictionary.

Click Here to learn all about Dictionary

Example


# list of python dictionary
students = [{'age': 21, 'course': 'MCA', 'name': 'Vishvajit'},
 {'age': 23, 'course': 'BTech', 'name': 'John'},
 {'age': 24, 'course': 'MCA', 'name': 'Shanaya'}]
 
 # access name of each dictionary
for i in students:
 print(i.get("age"))

Output


21
23
24

JSON ( JavaScript Object Notation )

JSON stands for JavaScript Object Notation. JSON is a file format and data interchange format that stores human-readable text. Basically, JSON is used to send data to the server or receive data from the server.JSON is a language-independent file format derived from JavaScript but nowadays many programming languages provide the facility to parse the data into JSON format.

Let’s see simple JSON example data.

Example


[
  {
    "age": 21,
    "course": "MCA",
    "name": "Vishvajit"
  },
  {
    "age": 23,
    "course": "BTech",
    "name": "John"
  },
  {
    "age": 24,
    "course": "MCA",
    "name": "Shanaya"
  }
]

As you can see in the above example, we have simple JSON data.

Python Program To Convert JSON To Python Object

Python JSOM module provides a loads() function to convert JSON to Python Object.

  • json.loads()

json.loads() the method is used to convert JSON string data to Python objects. this function takes JSON string as a parameter and converts it into a Python object.

Let’s convert simple JSON data to a Python dictionary using json.loads() function.

Convert SimpleJSON to Python Dictionary

Here we are going to convert simple JSON data to a Python dictionary.


import json

# json data
students = '{"age": 21, "course": "MCA", "name": "Vishvajit"}'

# convert to python dictionary
dict_data = json.loads(students)
print(dict_data)

Output

{'age': 21, 'course': 'MCA', 'name': 'Vishvajit'}
Python Tutorials

Convert list of objects ( JSON ) to Python Object

Here we are going to large JSON ( List contain multiple objects ) into a Python Object.

Example


import json
from pprint import pprint

# json data
students = '''[
  {
    "age": 21,
    "course": "MCA",
    "name": "Vishvajit",
    "address": ["Noida"]
    
  },
  {
    "age": 23,
    "course": "BTech",
    "name": "John",
    "address": ["Noida", "Mumbai"]
  },
  {
    "age": 24,
    "course": "MCA",
    "name": "Shanaya",
    "address": ["Noida", "Lucknow", "New Delhi"]
  },
  
    {
    "age": 26,
    "course": "MBA",
    "name": "Shashank",
    "address": ["Noida", "Lucknow"]
  },
  
    {
    "age": 28,
    "course": "MTech",
    "name": "Hari",
    "address": ["Noida", "New Delhi"]
  }
]'''

# convert to python dictionary
dict_data = json.loads(students)
pprint(dict_data)
print("-------------------------")
# access name and age of each object
for obj in dict_data:
    print(obj.get('name'), obj.get("age"))

Output

[{'address': ['Noida'], 'age': 21, 'course': 'MCA', 'name': 'Vishvajit'},
 {'address': ['Noida', 'Mumbai'], 'age': 23, 'course': 'BTech', 'name': 'John'},
 {'address': ['Noida', 'Lucknow', 'New Delhi'],
  'age': 24,
  'course': 'MCA',
  'name': 'Shanaya'},
 {'address': ['Noida', 'Lucknow'],
  'age': 26,
  'course': 'MBA',
  'name': 'Shashank'},
 {'address': ['Noida', 'New Delhi'],
  'age': 28,
  'course': 'MTech',
  'name': 'Hari'}]
-------------------------
Vishvajit 21
John 23
Shanaya 24
Shashank 26
Hari 28

Now you can perform any operations with dict_data because now it is converted into a python object.

Convert JSON Data From a File to Python Object

Sometimes you have a JSON file that contains a large amount of JSON data and you want to convert that data to a Python object then you can following the approach.
Suppose we have a JSON file named data.json that contains some JSON data.

Note:- To convert JSON file data to Python object, Python provides us another function named json.load() which takes the file pointer as a parameter.

data.json


[
  {
    "age": 21,
    "course": "MCA",
    "name": "Vishvajit",
    "address": ["Noida"]
    
  },
  {
    "age": 23,
    "course": "BTech",
    "name": "John",
    "address": ["Noida", "Mumbai"]
  },
  {
    "age": 24,
    "course": "MCA",
    "name": "Shanaya",
    "address": ["Noida", "Lucknow", "New Delhi"]
  },
  
    {
    "age": 26,
    "course": "MBA",
    "name": "Shashank",
    "address": ["Noida", "Lucknow"]
  },
  
    {
    "age": 28,
    "course": "MTech",
    "name": "Hari",
    "address": ["Noida", "New Delhi"]
  }
]

Python Script


import json
from pprint import pprint
# load json file
with open("data.json", "r") as file:
    data = json.load(file)

pprint(data)

Note:- In the above example file represent the file pointer.

Output


[{'address': ['Noida'], 'age': 21, 'course': 'MCA', 'name': 'Vishvajit'},
 {'address': ['Noida', 'Mumbai'], 'age': 23, 'course': 'BTech', 'name': 'John'},
 {'address': ['Noida', 'Lucknow', 'New Delhi'],
  'age': 24,
  'course': 'MCA',
  'name': 'Shanaya'},
 {'address': ['Noida', 'Lucknow'],
  'age': 26,
  'course': 'MBA',
  'name': 'Shashank'},
 {'address': ['Noida', 'New Delhi'],
  'age': 28,
  'course': 'MTech',
  'name': 'Hari'}]

Summary

So in this article, we have seen all about how to convert JSON String to Python dictionary with the help of json.loads() and json.load() function. Here we have covered both the options ( Simple JSON data and JSON file data ) to convert JSON data to Python dictionary. I hope after this article, you don’t have any confusion regarding convert JSON to Python object.

If this article is helpful for you, please share it and keep visit for further interesting Python tutorials.

Resource:- Click Here

Thanks for reading…

How to Convert Python Dictionary to JSON
How To Sort Python Dictionary by Key or Value

Related Posts