Convert from JSON to Python object

By IncludeHelp Last updated : February 20, 2024

To convert a JSON to a Python object, you can use the json.loads() method by passing a JSON string. The json.loads() method parses a valid JSON string and converts it into a Python object (dictionary).

Python program to convert JSON to an object

This example demonstrates converting from JSON to Python object.

# Import the json module
import json

# Creating a JSON string
student = '{"id":"101", "std_name": "Alex", "course":"MCA"}'
# Printing value and types of 'student'
print("student :", student)
print("Type of student :", type(student))

# Converting JSON string to Python object
result = json.loads(student)

print("After converting...")
print("result :", result)
print("Type of result :", type(result))

Output

The output of the above program is:

student : {"id":"101", "std_name": "Alex", "course":"MCA"}
Type of student : <class 'str'>
After converting...
result : {'id': '101', 'std_name': 'Alex', 'course': 'MCA'}
Type of result : <class 'dict'>

To understand the above program, you should have the basic knowledge of the following Python topics:

 
 

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.