Json is a efficient lightweight data format, widely used in fields including data exchange over internet.
Its data structure is similiar to dictionary in Python.
Python json
module provides easy parsing and coding of json data.
To generate a json object:
>>> import json >>> x = json.loads('{"USA":3,"India":11,"Brazil":1}') >>> x
{'Brazil': 1, 'India': 11, 'USA': 3}
>>> x['Brazil']
1Suppose we have a json file "tp.json":
{ "Mike": { "name": "Mike Johnson", "age": 32 } "Jeff": { "name": "Jeffrey Smith", "age": 27 } "Lynn": { "Lynn Martin", "age": 54 } }Following Python code convert the json into dictionary and print out:
>>> import json >>> fl = open('tp.json').read() >>> jn = json.loads(fl) >>> for key in jn: ... print(key) ... for key2 in jn[key]: ... print(key2, ": ", jn[key][key2]) ...
Lynn name : Lynn Martin age : 54 Jeff name : Jeffrey Smith age : 27 Mike name : Mike Johnson age : 32