January 2019
Intermediate to advanced
390 pages
9h 16m
English
To load and decode JSON data, use the json.load() or json.loads() functions. As an example, the following code reads the first 10 lines from the zips.json file and prints them nicely:
import osimport jsonfrom pprint import pprintwith open(os.path.join(data_folder,data_file)) as json_file: for line,i in zip(json_file,range(10)): json_data = json.loads(line) pprint(json_data)
The objects are printed as follows:
{'_id': '01001',
'city': 'AGAWAM',
'loc': [-72.622739, 42.070206],
'pop': 15338,
'state': 'MA'}
The json.loads() function takes string objects as input while the json.load() function takes file objects as input. Both functions decode the JSON object and load it in the json_data file as a Python ...
Read now
Unlock full access