How to read a json file directly into a Pandas DataFrame in Python

1 min

You might came up needing to read a JSON file and get it in a DataFrame format.

This for various reasons, but most obviously the ease of manipulation.

To do so, you will need a JSON with records in it.

Here is an example

[
    {"firstname" : "Jason",
     "lastname" : "Smith",
     "age" : 28,
     "city" : "Melbourne",
     "studies" : "Psychology"
    },
    {"firstname" : "Sofia",
     "lastname" : "Johnson",
     "age" : 24,
     "city" : "Sydney",
     "studies" : "Law"
    },
    {"firstname" : "John",
     "lastname" : "Bly",
     "age" : 31,
     "city" : "New York",
     "studies" : "Marketing"
    }
]
Here is our my_json_file.json

Here is the code

import pandas as pd

# we read our json file
df = pd.read_json("my_json_file.json")
Here is the result dataframe

Here you are! You now know how to read a json directly into a pandas DataFrame!