Python Deserialize JSON String to Object
In this tutorial we will learn how to use Python built-in package json to deserialize a JSON string to a Python object.
First step you need to import json package.
import json
Declare a JSON string to deserialize, for example:
json_data = '{"firstName": "Larry", "lastName":"Scott", "email": "larry@toricode.com"}'
Using method json.loads() to deserialize the JSON string.
json_object = json.loads(json_data)
To observe the return value of json.loads() method we print the type of return object and its data as below.
print("Object type: ", type(json_object))
print("First Name: ", json_object['firstName'])
print("Last Name: ", json_object['lastName'])
print("Email Address: ", json_object['email'])
Below is the complete Python code of this tutorial.
import json
json_data = '{"firstName": "Larry", "lastName":"Scott", "email": "larry@toricode.com"}'
json_object = json.loads(json_data)
print("Object type: ", type(json_object))
print("First Name: ", json_object['firstName'])
print("Last Name: ", json_object['lastName'])
print("Email Address: ", json_object['email'])
Execute the Python code above on your terminal to get result.