How to convert JSON data into a Python object?


JSON can be abbreviated as JavaScript Object Notation. Json means a script of a text file in a programming language to transfer and store the data.

  • Json supported by the python programming language using a built-in package named json.

  • The Json text is given in the quoted string format which contains in the key and value within the curly braces{}.

  • This looks like a dictionary format in python programming language.

For using this json package in the python programming language we have to import the json package in python script. In the Json package we have so many methods, among them one of the methods is loads.

This is used to convert the Java objects into the python objects. This is a class of the dump method in Json Package.

The loads() method is used to when the data to be in string format and required to be printed whereas the dump is used to store the data.

Syntax

The following is the syntax of the loads method of the json package that we use in the python programming language

variable_name = json.loads(string)

Where,

  • variable_name is the name of the variable.

  • json is the name of the package.

  • loads is the name of the method.

  • string is the string to be converted in the python object.

Example

Let’s see an example to understand the process to load the json string into the python object format. The following code can be used to load the code.

import json
jstring = '{"roll":84,"name":"Swapna","branch":"ECE"}'
print(type(jstring))
pyobj = json.loads(jstring)
print(pyobj)
print(type(pyobj))

Let’s see the code in detail. We imported the json package at first for using it in python programming language. After that created a json string object in the string format and assigned to the variable jstring. After that printed the type of the jstring. Next applied the json method lads function on the jstring to convert it into the python object and assign the output to the variable pyobjc. Next printed the output of the pyobj and the type of the pyobj.

Output

The following is the output of the json package loads method. In the output we can see the converted json format into python object dictionary.

<class 'str'>
{'roll': 84, 'name': 'Swapna', 'branch': 'ECE'}
<class 'dict'>

Example

Let’s see another example to understand the process to load the json string into the python object format. The following code can be used to load the code.

import json
jstring = '{"brand":"Hyundai", "year":2010}'
print(type(jstring))
pyobj = json.loads(jstring)
print(pyobj)
print(type(pyobj))

Let’s see the code in detail. We imported the json package at first for using it in python programming language. After that created a json string object in the string format and assigned to the variable jstring.

After that printed the type of the jstring. Next applied the json method lads function on the jstring to convert it into the python object and assign the output to the variable pyobjc. Next printed the output of the pyobj and the type of the pyobj.

Output

The following is the output of the json package loads method. In the output we can see the converted json format into python object dictionary.

<class 'str'>
{'brand': 'Hyundai', 'year': 2010}
<class 'dict'>

Example

Let’s see another example to understand the process to load the json string into the python object format. The following code can be used to load the code.

import json
jstring = '{"name":"biryani"}'
print(type(jstring))
pyobj = json.loads(jstring)
print(pyobj)
print(type(pyobj))

Let’s see the code in detail. We imported the json package at first for using it in python programming language. After that created a json string object in the string format and assigned to the variable jstring.

After that printed the type of the jstring. Next applied the json method lads function on the jstring to convert it into the python object and assign the output to the variable pyobjc. Next printed the output of the pyobj and the type of the pyobj.

Output

The following is the output of the json package loads method. In the output we can see the converted json format into python object dictionary.

<class 'str'>
{'name': 'biryani'}
<class 'dict'>

Updated on: 15-May-2023

680 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements