Pandas Read JSON allows you to read JSON files and convert their data into a DataFrame. JSON (JavaScript Object Notation) is a popular format used to store and exchange data between applications. In this chapter, you will learn how to read JSON files in Python using Pandas and work with their data.
What is JSON?
JSON stands for JavaScript Object Notation. It is a lightweight data format used to store and transfer information.
JSON data is written using key-value pairs and can contain objects, arrays, strings, numbers, and other data types.
Example of JSON Data:
{
"Name": ["Amit", "Neha", "Rahul"],
"Age": [21, 22, 20],
"Marks": [85, 92, 78]
}
In this example:
Name,Age, andMarksare keys.- The values are stored as lists.
- Each list contains three values.
Reading JSON Files in Pandas
Pandas provides the read_json() function to read JSON files and convert their data into a DataFrame.
Syntax:
pd.read_json("filename.json")
Here, filename.json is the name of the JSON file you want to read.
Example:
First, create a file named students.json and add the following data:
{
"Name": ["Amit", "Neha", "Rahul"],
"Age": [21, 22, 20],
"Marks": [85, 92, 78]
}
Now, read this JSON file using Pandas:
import pandas as pd
df = pd.read_json("students.json")
print(df)
Output:
Name Age Marks
0 Amit 21 85
1 Neha 22 92
2 Rahul 20 78
Explanation: The read_json() function reads the JSON file and converts its data into a Pandas DataFrame.
Reading JSON Files Using a Full File Path
Sometimes, your JSON file may be stored in another folder. In that case, you can provide its complete file path.
Example:
import pandas as pd
df = pd.read_json("C:/Users/Admin/Documents/students.json")
print(df)
Output:
Name Age Marks
0 Amit 21 85
1 Neha 22 92
2 Rahul 20 78
Explanation: The full file path tells Pandas exactly where the JSON file is stored. Change the path according to your computer.
Reading JSON Data in Records Format
JSON data is also commonly stored as a list of objects. Each object represents one record, just like a row in a table.
Example of JSON Data:
Create a file named employees.json:
[
{
"Name": "Amit",
"Department": "IT",
"Salary": 35000
},
{
"Name": "Neha",
"Department": "HR",
"Salary": 40000
},
{
"Name": "Rahul",
"Department": "Sales",
"Salary": 32000
}
]
Read the JSON file:
import pandas as pd
df = pd.read_json("employees.json")
print(df)
Output:
Name Department Salary
0 Amit IT 35000
1 Neha HR 40000
2 Rahul Sales 32000
Explanation: Pandas reads each JSON object as a row and its keys as column names.
Reading JSON Data from a Python String
You can also read JSON data stored inside a Python string. For this, use StringIO from Python’s io module.
Example:
import pandas as pd
from io import StringIO
json_data = """
[
{"Name": "Amit", "Marks": 85},
{"Name": "Neha", "Marks": 92},
{"Name": "Rahul", "Marks": 78}
]
"""
df = pd.read_json(StringIO(json_data))
print(df)
Output:
Name Marks
0 Amit 85
1 Neha 92
2 Rahul 78
Explanation: StringIO() allows Pandas to read a JSON string as if it were a file.
Student Task
Create a JSON file named products.json with the following data:
{
"Product": ["Laptop", "Mouse", "Keyboard", "Monitor"],
"Price": [55000, 800, 1500, 12000],
"Stock": [10, 50, 30, 15]
}
Your Tasks:
- Import the Pandas library.
- Read the
products.jsonfile usingpd.read_json(). - Store the result in a variable named
df. - Display the complete DataFrame.
- Check whether all product names and prices are displayed correctly.
Key Takeaways
- JSON stands for JavaScript Object Notation.
- JSON is commonly used to store and exchange data.
- Pandas provides the
read_json()function to read JSON files. pd.read_json()converts JSON data into a DataFrame.- JSON data can be stored as key-value structures or lists of objects.
StringIO()can be used to read JSON data stored in a Python string.
Here are SEO-friendly FAQs for your Pandas Read JSON Tutorial. These questions naturally include your focused keywords and are written to help improve search engine visibility.
Frequently Asked Questions (FAQs)
1. What is Pandas Read JSON in Python?
Pandas Read JSON is a function used to read JSON (JavaScript Object Notation) data and convert it into a Pandas DataFrame. It makes it easy to analyze, organize, and manipulate JSON data in Python.
2. How to Read JSON in Pandas?
You can read JSON in Pandas using the pd.read_json() function. First, import the Pandas library and then use the function to load JSON data into a DataFrame.
import pandas as pd
df = pd.read_json("data.json")
print(df)
3. How to Read a JSON File in Python Using Pandas?
To read a JSON file in Python using Pandas, use the pd.read_json() function and provide the JSON file path. Pandas automatically converts the JSON data into a DataFrame.
4. What is the Syntax of Pandas Read JSON File?
The basic syntax for reading a JSON file in Pandas is:
pd.read_json(path, orient=None)
Here, path specifies the JSON file location, while orient defines the expected JSON data format.
5. Can Pandas Read JSON Files Directly?
Yes, Pandas can read JSON files directly using the pd.read_json() function. You do not need to manually convert JSON data into a CSV file before loading it into a DataFrame.
6. What is the Difference Between JSON and CSV Files in Pandas?
JSON files store data in key-value pairs and support nested structures, while CSV files store data in rows and columns. Pandas provides pd.read_json() for JSON files and pd.read_csv() for CSV files.
7. How to Read a JSON File from a URL in Python?
You can read a JSON file from a URL using the Pandas pd.read_json() function. Simply provide the URL containing the JSON data.
import pandas as pd
df = pd.read_json("https://example.com/data.json")
print(df)
8. Which Function Is Used to Pandas Read JSON Files in Python?
The read_json() function from the Pandas library is used to read JSON files in Python. It loads JSON data and converts it into a DataFrame for further analysis.
9. Can We Convert a JSON File into a Pandas DataFrame?
Yes, you can easily convert a JSON file into a Pandas DataFrame using pd.read_json(). This function helps you work with JSON data using Pandas DataFrame operations.
10. Why Do We Use Pandas Read JSON in Data Analysis?
Pandas Read JSON is useful in data analysis because it allows developers and data analysts to import JSON data, organize records into DataFrames, and perform data cleaning, filtering, and analysis.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
