How to Convert a DataFrame to JSON in Python? [4 ways]

In this Python tutorial, I will tell you how to convert a dataframe to JSON in Python using different methods with examples.

As we all know, data conversion is a common task in Python. JSON (JavaScript Object Notation) is a lightweight data-interchange format, generally used for data storage and communication between web servers and clients.

We can use the to_json() and json_normalize() functions, or we can also use different modules like json and simplejson to convert a dataframe to JSON file in Python.

Let’s see them one by one using some illustrative examples:

Convert a dataframe to JSON in Python using the to_json() method

The simplest and most straightforward method of converting a Pandas DataFrame to JSON is the to_json() method. The to_json() method converts the DataFrame to a JSON.

We can use the orient parameter, which specifies the format in which the Pandas dataframe will be converted to a JSON string in Python. This allows us to customize the output format in Python.

Here is an example of Pandas dataframe to JSON list in Python:

import pandas as pd

Population_data = pd.DataFrame({'name': ['USA', 'Brazil'], 'population': [336157983, 30216422446]})
json_population_data = Population_data.to_json(orient='records')
print(json_population_data)

Here what we are doing is: First, we import the library that we are going to use.

import pandas as pd

Create a DataFrame object with two columns, name and population, and two rows of data using pd.DataFrame() method.

df = pd.DataFrame({'name': ['USA', 'Brazil'], 'population': [336157983, 30216422446]})

The to_json() method in Pandas of the DataFrame object is converted into a JSON string with ‘records‘ as the orientation in Python. The resulting JSON string is assigned to the json_data variable.

json_data = df.to_json(orient='records')

Output:

[{"name":"USA","population":336157983},{"name":"Brazil","population":30216422446}]

After implementing the code in the Pycharm editor, the screenshot is mentioned below.

How to Convert a DataFrame to JSON in Python

Pandas dataframe to json file using json_normalize()

The json_normalize() function from the Pandas library can be used to flatten a JSON object into a DataFrame. You can then convert the DataFrame to JSON using the to_json() method.

READ:  How to compare two lists in Python and return non-matches elements

Here is an example:

import pandas as pd
import json

json_data = '[{"name":"USA","pop":336157983},{"name":"Brazil","pop":30216422446}]'
df = pd.json_normalize(json.loads(json_data))
json_output = df.to_json(orient='records')
print(json_output)
print(type(json_output))

We are using the json.load() to load a JSON string into a Python object, and then we normalize it into a Pandas dataframe in Python using the pd.json_normalize().

json_output = df.to_json(orient='records')

Output: I am also printing the type of the JSON output data using the type() function in Python to confirm that we have successfully converted the dataframe into a JSON.

[{"name":"USA","pop":336157983},{"name":"Brazil","pop":30216422446}]
<class 'str'>

The screenshot below features the output after executing the Pycharm editor’s code.

dataframe to json in Python pandas

Pandas dataframe to json using the json module

Python has the json module that provides different functions for encoding and decoding JSON data. We can convert a dataframe to a JSON string using the json.dumps() function.

Here is the example for converting a dataframe to a JSON file in Python:

import pandas as pd
import json

df = pd.DataFrame({'name': ['USA', 'Brazil'], 'population': [336157983, 30216422446]})
json_data = json.dumps({'data': df.values.tolist(), 'columns': df.columns.tolist()})
print(json_data)
print(type(json_data))

The code imports pandas and json libraries. Then create a DataFrame object with two columns, name and population, and two rows of data representing the USA and Brazil using the pd.DataFrame() function in Python.

json.dumps({'data': df.values.tolist(), 'columns': df.columns.tolist()})
  • We use the to_dict() method in Python to convert the DataFrame object into a dictionary, where each key represents a column name, and each value represents a list of the column’s values.
  • This dictionary is then transformed into a new dictionary with two keys: data and columns. The data key maps to a list of lists that contains the data in the DataFrame, and the columns key maps to a list of column names.
  • Finally, the json.dumps() method is used to convert this dictionary into a JSON string.
READ:  Pandas count rows with condition in Python [9 different ways]

Output:

{"data": [["USA", 336157983], ["Brazil", 30216422446]], "columns": ["name", "population"]}

After the code was implemented in the Pycharm editor, the subsequent screenshot was taken to illustrate the output.

pandas to json in Python

Python Convert Dataframe to Json using the simplejson module

The simplejson module is a third-party module in Python that provides a faster and more efficient way to encode and decode JSON in Python.

It is a drop-in replacement for the standard json module, offering better performance in some cases in Python.

Here is an instance:

Note: Please install the simplejson library to use the functions to convert a Pandas dataframe to json in Python.

pip install simplejson

Here is the full code:

import pandas as pd
import simplejson as json

df = pd.DataFrame({'name': ['USA', 'Brazil'], 'population': [336157983, 30216422446]})
json_d = json.dumps(df.to_dict(orient='records'))
print(json_d)
print(type(json_d))

Output:

[{"name": "USA", "population": 336157983}, {"name": "Brazil", "population": 30216422446}]
<class 'str'>

Below is a screenshot that captures the outcome after implementing the Pycharm editor’s code.

create json from dataframe python

Conclusion

This Python tutorial explains how to convert a dataframe to JSON in Python using four different methods: the to_json() and json_normalize() functions and json and simplejson modules.

Hopefully, you understand all of these and will use them according to your requirements.

You may also like to read the following Python tutorials.