How to read a JSON file in Python

1 min readDataJsonDataFrameAPI
7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

You will at some point encounter JSON data.

It is the most widely used format for communication and transmission.

There are two ways to read a JSON file that I use most of the time.

It really depends on the structure of the file.

Reading JSON with the JSON library

When I encounter a JSON file that looks like this and is a straight dictionary, I use the JSON library like so

{
    "firstname" : "Brice",
    "lastname" : "Doe",
    "age" : 20,
    "has_siblings" : True,
    "favorite_quote" : """
    The world as we have created it is a process of our thinking.
    It cannot be changed without changing our thinking. Albert Einstein
    """
}
A JSON file that starts with a { or dict format - file.json

and

import json # We import the JSON library

with open("path/to/file.json", "r") as file:
    data = json.load(file) # we load the file to the data variable
    
print(data) # We check the data we have is alright
We read the JSON file

Reading JSON with pandas library

You might encounter other JSON-like formats, like a JSON file containing a list of records. This is often the case when dealing with APIs.

Such file would look like this :

[
{"firstname":"George","lastname":"Dubois"},
{"firstname":"Aman","lastname":"Van Hein"},
{"firstname":"Dennis","lastname":"Washington"},
{"firstname":"John","lastname":"Doe"},
{"firstname":"Foe","lastname":"Less"}
]
A JSON file that looks like a list of records. - file_2.json

Then I would read it like so

import pandas as pd # we import the pandas library to read it as dataframe

df = pd.read_json("path/to/file_2.json") # We read the file as dataframe

print(df) # We print the dataframe object
We read the file_2.json using the Pandas library

Here you are! You are now an expert at reading JSON files.

This can also be applied when you deal with APIs data !

7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Free Newsletter

Master Data Science in Days, Not Months 🚀

Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚

Weekly simple and practical lessons
Access to ready to use code examples
Skip the math, focus on results
Learn while drinking your coffee

By subscribing, you agree to receive our newsletter. You can unsubscribe at any time.