JavaScriptObjectNotation (JSON) is another popular data format in IoT systems. In this section, we will learn how to read JSON data with Python's JSON, NumPy, and pandas packages.
For this section, we will use the zips.json
file, which contains US ZIP codes with city codes, geolocation details, and state codes. The file has JSON objects recorded in the following format:
{ "_id" : "01001", "city" : "AGAWAM", "loc" : [ -72.622739, 42.070206 ], "pop" : 15338, "state" : "MA" }
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 thezips.json
file and prints them nicely:
import os import json from pprint import pprint with 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...