How to Convert One JSON Format to Another Using Python

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:

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:

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:

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:

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

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.