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.jsonfile 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 json.loads()function takes string objects as input while thejson.load()functiontakes file objects as input. Both functions decode the JSON object and load it in thejson_data file as a Python dictionary object.
The json.dumps()function takes an object and produces a JSON string, and the json.dump()function takes an object and writes the JSON string to the file. Thus, both these function do the opposite of the json.loads() and json.load() functions.