Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

How to Read a JSON File in Python

Read A Json File In Python

In this article, we’ll take a look at how to read a JSON file in Python.

Often, you may want to easily read and parse json data from relevant json files. Let’s find out some of the ways through which we can easily read and extract out this data!


Method 1: Using json.load() to read a JSON file in Python

The json module is a built-in module in Python3, which provides us with JSON file handling capabilities using json.load().

We can construct a Python object after we read a JSON file in Python directly, using this method.

Assume sample.json is a JSON file with the following contents:

{
"name": "AskPython",
"type": "website",
"language": "Python"
}

We can load the json objects into a Python object using the below program. We can now easily access it using {key: value} pair mappings of a dictionary!

import json
 
with open("sample.json", "r") as rf:
    decoded_data = json.load(rf)
 
print(decoded_data)
# Check is the json object was loaded correctly
try:    
    print(decoded_data["name"])
except KeyError:
    print("Oops! JSON Data not loaded correctly using json.loads()")

Output

{'name': 'AskPython', 'type': 'website', 'language': 'Python'}
AskPython

Indeed, we were able to get our JSON objects loaded correctly from our file!


Method 2: Use ijson for large JSON files

If your JSON file is large enough such that it is expensive to bring the whole content to memory, a better approach would be convert the file content into streams using ijson.

A stream is a collection of objects (just as JSON objects) which will be loaded onto memory only on demand. This means that our data loader is loading data “lazily”, i.e, only when needed.

This relaxes the memory requirement when working with large files. The content of the stream is stored in a temporary buffer, which makes it possible to deal with gigabytes of JSON files!

To install ijson, use pip!

pip install ijson

Now, to experiment, we’ll use a somewhat small JSON file, since it’ll be time consuming to download gigabytes of data!

I’ll be using the COVID timeseries JSON file, on this link. Download the file, and rename this as covid_timeseries.json. The file size must be about 2 MB.

import ijson

for prefix, type_of_object, value in ijson.parse(open("covid_timeseries.json")):
    print(prefix, type_of_object, value)

Sample Output (Few Lines)

Yemen.item.date string 2020-4-13
Yemen.item map_key confirmed
Yemen.item.confirmed number 1
Yemen.item map_key deaths
Yemen.item.deaths number 0
Yemen.item map_key recovered
Yemen.item.recovered number 0
Yemen.item end_map None
Yemen.item start_map None
Yemen.item map_key date
Yemen.item.date string 2020-4-14
Yemen.item map_key confirmed
Yemen.item.confirmed number 1
Yemen.item map_key deaths
Yemen.item.deaths number 0
Yemen.item map_key recovered
Yemen.item.recovered number 0
Yemen.item end_map None
Yemen end_array None

This will print the contents of the huge JSON file, but you can keep a counter variable to avoid printing the entire file.

While ijson may be slow, it seems to operate within a lower memory bound. You can try this module if you’re working with large files.


Conclusion

In this article, we learned how we could read a JSON file in Python. We also briefly looked at handling a larger volume of data using ijson.


References


Leave a Reply

Your email address will not be published. Required fields are marked *