JSON: Parse JSON in Python - detailed deserializing
Deserialization is the opposite of Serialization, which is the conversion of JSON objects into their respective Python objects (reading it). The load() method is used for it. If you have used JSON data from another program or obtained as a string format of JSON, then it can easily be deserialized with load(), which is usually used to load from a string. Otherwise, the root object is in list or dict.
with open("Sample.json", "r") as read_it:
data = json.load(read_it)
The json module makes it easy to parse JSON strings and files containing JSON objects.
Example 1: Python JSON to dict
You can parse a JSON string using the json.loads() method. The method returns a dictionary.
import json
cake = '{"name": "Banana miracle", "Toppings": ["Chocolate", "Mint Frosting"]}'
cake_dict = json.loads(cake)
# Output: {'name': 'Banana miracle', 'Toppings': ['Chocolate', 'Mint Frosting']}
print(cake_dict)
# Output: ['Chocolate', 'Mint Frosting']
print(cake_dict['Toppings'])
Here, cake is a JSON string, and cake_dict is a dictionary.
Example 2: Python read JSON file
You can use the json.load() method to read a file containing a JSON object. Suppose, you have a file named cake.json, which contains a JSON object.
{"name": "Bob",
"toppings": ["Chocolate", "Mint frosting"]
}
# Here's how you can parse this file:
import json
with open('path_to_file/cake.json') as f:
data = json.load(f)
# Output: {'name': "Banana miracle", "Toppings": ["Chocolate", "Mint Frosting"]}
print(data)
Here, we have used the open() function to read the JSON file. Then, the file is parsed using the json.load() method, which gives us a dictionary named data.
If you do not know how to read and write files in Python, check the Python documentation: Python File I/O.
Python Convert to JSON string: You can convert a dictionary into a JSON string using the json.dumps() method.
Example 3: Convert dict to JSON
import json
cake_dict = {'name': 'Banana miracle',
'price': 35,
'Filling': None
}
cake_json = json.dumps(cake_dict)
# Output: {"name": "Banana miracle", "price": 35, "Filling": null}
print(cake_json)
You can convert the Python objects listed in the following table into JSON strings. When you convert from Python to JSON, Python objects are converted into the JSON (JavaScript) equivalent.
Python | JSON |
---|---|
dict |
Object |
list |
Array |
tuple |
Array |
str |
String |
int |
Number |
float |
Number |
True |
true |
False |
false |
None |
null |
The following example shows how to convert Python objects into JSON strings and print the values.
Example
import json
cake = {
"name": "Banana miracle",
"price": 35,
"Baked": True,
"Frozen": False,
"Size": ("small","big"),
"Filling": None,
"Toppings": [
{"topping": "Chocolate", "additional price": 2.5},
{"topping": "Mint Frosting", "additional price": 4.1}
]
}
# convert into JSON:
y = json.dumps(cake)
# the result is a JSON string:
print(y)
Result
{"name": "Banana miracle", "price": 35, "Baked": true, "Frozen": false, "Size": ["small", "big"], "Filling": null, "Toppings": [{"topping": "Chocolate", "additional price": 2.5}, {"topping": "Mint Frosting", "additional price": 4.1}]}