This tutorial will guide you on how to convert one JSON data format to another using Python. JSON stands for JavaScript Object Notation, a popular data format that is particularly suitable for web-based data exchange due to its lightweight structure and easy readability.
Python’s built-in libraries such as json and pandas make it easy to work with JSON data. This tutorial assumes that you have some basic understanding of Python.
Step 1: Importing Necessary Libraries
Firstly, we need to import the required Python libraries. For this task, we will be using the json and pandas libraries. Use the following commands to import:
1 2 |
import json import pandas as pd |
Step 2: Load JSON Data
The next step is to load the JSON data. We will be using a JSON file for this purpose. Assume we have a json file named ‘data.json’ which contains our initial JSON data:
{ "Employees": [ {"id": 1, "name": "John", "dept": "Sales"}, {"id": 2, "name": "Jane", "dept": "Marketing"} ] }
Using the json library, we can load the JSON data into a Python dictionary:
1 2 |
with open('data.json') as f: data = json.load(f) |
Step 3: Transform JSON Data
The next step is to convert our Python dictionary ‘data’ into a pandas DataFrame, where we can easily filter or modify our data:
1 |
df = pd.DataFrame(data['Employees']) |
Step 4: Save the Data to a New JSON Format
Finally, save your data into the new JSON format. We can use the pandas method to_json for this purpose:
1 |
df.to_json('new_data.json', orient='records') |
This will create a new file, ‘new_data.json’, with the JSON data in the following format:
[ {"id": 1, "name": "John", "dept": "Sales"}, {"id": 2, "name": "Jane", "dept": "Marketing"} ]
The Full Code
1 2 3 4 5 6 7 8 9 |
import json import pandas as pd with open('data.json') as f: data = json.load(f) df = pd.DataFrame(data['Employees']) df.to_json('new_data.json', orient='records') |
Conclusion
With just a few lines of Python code, we can transform and convert JSON data between different formats. Understanding how to manipulate JSON data with Python is a helpful skill, especially for data scientists or data engineers, as it is a widely adopted format for data exchange in web-based applications.