Reading a JSON File in Python

Python Code

Reading a JSON file in Python is possible using json.load():

import json

with open('PATH/TO/YOUR/FILE.json') as file:
    data = json.load(file)

Example of Reading a JSON File in Python

For example, suppose you have a JSON file called employee.json:

{  
    "employee": {  
        "name": "Charlie",   
        "salary": 3200,   
        "married": true,
        "address": "Imaginary Street 149"  
    }  
}  

And let’s say it’s in the same folder as your program file:

In this case, you can load the JSON such that the path to the file is just the name of the file:

import json

with open('employee.json') as file:
    data = json.load(file)

print(data)

(Remember, if the file is not in the same folder, you need to figure out the correct path to the file.)

Here is the output of the above program:

{'employee': {'name': 'Charlie', 'salary': 3200, 'married': True, 'address': 'Imaginary Street 149'}}

In short, this code:

  • Opens a file using the with statement.
  • Loads the JSON content from that file using the json.load() method. This turns the JSON data into a Python dictionary.
  • Finally, it prints the JSON data dictionary into the console.

Now you can do whatever you want with the JSON data dictionary.

For example, let’s print the address of the employee that comes with the data:

address = data["employee"]["address"]
print(address)

Output:

Imaginary Street 149

So this is how easy it really is to read a JSON file into your Python program. Before you go, there’s one thing that can help you tremendously when reading JSON. It’s called pretty printing.

Make Your JSON Data Readable with Pretty-Printing

Now that you’ve read your first JSON file in Python, you may notice the format is not particularly reader-friendly.

Luckily, you can pretty-print JSON to make it look more appealing for you.

To do this, use the json.dumps() method. Don’t forget to specify the number of spaces you want for indentations:

print(json.dumps(data, indent=4))

Let’s apply this to the previous example and see what happens.

Example of Pretty-Printing a JSON File in Python

Let’s go back to the previous example with employee.json file.

Let’s read the JSON file the same way you already saw. But instead of directly printing the JSON data, let’s pretty-print it to make it readable:

import json

with open("employee.json") as file:
    data = json.load(file)
   
print(json.dumps(data, indent=4))

Output:

{
    "employee": {
        "name": "Charlie",
        "salary": 3200,
        "married": true,
        "address": "Imaginary Street 149"
    }
}

Now the output is way easier to understand than in the previous example as it’s now printed in a clear format.

When reading JSON, I highly recommend you to pretty-print it to make it easier for you to work with.

Conclusion

To read JSON data into your Python program, use json.dumps() method. Before you do that, you need to:

  • Have a JSON file (and know the path to it).
  • Import the json module into your Python program.

For example, if you have a JSON file employee.json in the same folder as your Python program, you can read it with:

import json

with open('employee.json') as file:
    data = json.load(file)

Also, don’t forget to pretty-print the JSON data to make it readable.

Thanks for reading.

Happy coding!

Further Reading

You commonly need to deal with JSON data when using an API. Make sure you check out a comprehensive guide on what is an API.

Share on facebook
Facebook
Share on google
Google+
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest

Leave a Comment

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