The JSON Module in Python
JSON
JSON(JavaScript Object Notation) is a standard and lightweight format for data interchange. We can handle JSON data with the json module,
import json # input JSON person = '{ "name":"Ankit", "age":23, "city":"Delhi"}' # parse person a = json.loads(person) #get the results in a Python dict print(a['city']) #Delhi
Dump data into JSON
import json person={ 'name':'Ankit', 'age':23, 'city':'Delhi' } # change into JSON y = json.dumps(person) # print the JSON print(y) #Output {"name": "Ankit", "age": 23, "city": "Delhi"}
The Object types dict, list, tuple, string, int, float, True, False, and None can also be converted into JSON strings, with JSON module.
import json
print(json.dumps({'name': 'Amit', "maerks": 79}))
print(json.dumps(['Ajay', 78,90,True]))
print(json.dumps(('Orange', 'Apple','Grapes')))
print(json.dumps('Python Programming'))
print(json.dumps(73))
print(json.dumps(96.45))
print(json.dumps(True))
print(json.dumps(False))
print(json.dumps(None))
#Output
{"name": "Amit", "maerks": 79}
["Ajay", 78, 90, true]
["Orange", "Apple", "Grapes"]
"Python Programming"
73
96.45
true
false
null
Dump and Load data from files
We can retrieve and manipulate the data from/to a JSON file with the json module. For example to write the JSON data to a file,
import json d={ 'color':'RED', 'model':2003, 'brand':'Honda', 'dim':[1880,1400,780] } with open('C:/Users/user/Desktop/car','w') as f: json.dump(d,f) print('written')
To read the data from the JSON file, we can write
import json with open('C:/Users/user/Desktop/car','r') as f: d=json.load(f) print(d) #Output: {'color': 'RED', 'model': 2003, 'brand': 'Honda', 'dim': [1880, 1400, 780]} OR import json j = {"cars": [{"color": "BLACK", "brand": "HONDA"}, {"color": "RED", "brand": "RENAULT"}]} with open('C:/Users/user/Desktop/car','w') as f: d=json.dump(j,f) print('written')
To load the data from a file, we can use
import json j = {"cars": [{"color": "BLACK", "brand": "HONDA"}, {"color": "RED", "brand": "RENAULT"}]} with open('C:/Users/user/Desktop/car','r') as f: d=json.load(f) print(d)
import json j = {"cars": [{"color": "BLACK", "brand": "HONDA"}, {"color": "RED", "brand": "RENAULT"}]} with open('C:/Users/user/Desktop/car','r') as f: d=json.load(f) print(json.dumps(d,indent=2)) #Output: { "cars": [ { "color": "BLACK", "brand": "HONDA" }, { "color": "RED", "brand": "RENAULT" } ] }
We can also sort the keys, with sort_keys attribute,
import json
j = {"cars": [{"color": "BLACK", "brand": "HONDA"}, {"color": "RED", "brand": "RENAULT"}]}
with open('C:/Users/user/Desktop/car','r') as f:
d=json.load(f)
print(json.dumps(d,sort_keys=True,indent=2))
#Output:
{
"cars": [
{
"brand": "HONDA",
"color": "BLACK"
},
{
"brand": "RENAULT",
"color": "RED"
}
]
}
We can also use the separators attribute,
import json j = {"cars": [{"color": "BLACK", "brand": "HONDA"}, {"color": "RED", "brand": "RENAULT"}]} with open('C:/Users/user/Desktop/car','r') as f: d=json.load(f) print(json.dumps(d,sort_keys=True,indent=2, separators=(' . ', ' = '))) #Output: { "cars": [ { "brand"= "HONDA", "color"= "BLACK" }, { "brand"= "RENAULT", "color"= "RED" } ] }