![]() |
VOOZH | about |
JSON (JavaScript Object Notation) store data using key-value pairs. Reading JSON files using Pandas is simple and helpful when you're working with data in .json format. There are mainly three methods to read Json file using Pandas Some of them are:
The pd.read_json() function helps to read JSON data directly into a DataFrame. This method is used when we working with standard JSON structures. If the file is located on a remote server we can also pass the URL instead of a local file path. Letβs say you have a file named data.json with the following content:
[
{"id": 1, "name": "Alice", "age": 25},
{"id": 2, "name": "Bob", "age": 30},
{"id": 3, "name": "Charlie", "age": 22}
]
You can read this JSON file using the code below:
Output:
π Screenshot-2025-03-19-111811The json_normalize() is used when we are working with nested JSON structues. JSON from APIs often comes in nested form and this method helps to flatten it into a tabular format thatβs easier to work with in Pandas. This method is helpful when working with real-world JSON responses from APIs.
Output:
π Screenshot-2025-03-19-112119If JSON data is stored as a dictionary we can directly use pd.DataFrame() convert it into a structured DataFrame. This is helpful when you are working with pre-loaded or manually created JSON data in memory.
Output:
π Screenshot-2025-03-19-112239
These methods help you to use JSON data into Pandas for analysis and visualization. With just a few lines of code you can turn raw JSON into a clean and usable DataFrame.