read a json file in python

To read a JSON file in Python, you first need to open the file using the open() function with the mode set to r for read. Then, you can use the load() or loads() function from the json module to parse the JSON data into a Python object.

Here's an example code block that demonstrates this process:

main.py
import json

# open the JSON file
with open('example.json', 'r') as file:
    # load the JSON data into a Python object
    data = json.load(file)

# print the parsed data
print(data)
184 chars
10 lines

In the above code, replace example.json with the name of your JSON file, and the parsed data will be stored in the data object, which you can manipulate and work with as a regular Python object.

related categories

gistlibby LogSnag