Json Module
# Json = Java script object notation
# The json. load() is used to read the JSON document from file and The json.
# loads() is used to convert the JSON String document into the Python dictionary.
# fp file pointer used to read a text file, binary file or a JSON file that contains a JSON document.
import json
data = '{"Aryan": "Sonone" , "Vidhan": "Laddha"}'
print(data)
parsed = json.loads(data)
print(parsed['Aryan'])
data2 = {
"channel_name": "CodeWithHarry",
"cars": ['bmw', 'audi a8', 'ferrari'],
"fridge": ('roti', 540),
"isbad": False
}
jacomp = json.dumps(data2)
print(jacomp)
import json
x = {
"name": "Aryan",
"age": 14,
"married": False,
"pets": "Cats",
"cars": [
{"model": "BMW 230", "mpg": 27.5},
{"model": "Ford Edge", "mpg": 24.1}
]
}
# sort the result alphabetically by keys:
print(json.dumps(x, sort_keys=True))
Comments
Post a Comment