How to convert data to a JSON format using pandas

Overview

In Python, pandas is specifically designed for data manipulation to assist in data analysis. It offers many built-in data structures and operations to manipulate time series and tabular data. Similarly, DataFrame is a data structure just like tables, but it allows us to perform multiple operations on data.

We use to_json() to convert a DataFrame or Series to a valid JSONJavaScript Object Notation string.

Note: The datetime objects are converted into UNIX timestamps, while NaN values will be converted into null.

Syntax


# Signature
DataFrame.to_json(path_or_buf=None,
orient=None,
date_format=None,
double_precision=10,
force_ascii=True,
date_unit='ms',
default_handler=None,
lines=False,
compression='infer',
index=True,
indent=None,
storage_options=None)

Parameters

  • path_or_buf: This can be a string, path-like object, or None. It shows which type of results are returned by this function.
  • orient: This shows the standard JSON format. It can be index, column, split, records, values, or table.
  • date_format: This indicates the date conversion format. It can be epoch or iso.

Return value

Its return values depend on the path_or_buf argument. If path_or_buf=None, it returns JSONJavaScript Object Notation as a string. Otherwise, it returns false.

Example

# import libraries
import numpy as np
import pandas as pd
# convert a dictionary into a dataframe
df = pd.DataFrame({
"brand": ["Mercedies", "Audi", "Ford"],
"model": ["Benz", "A9" ,"Expedition"],
"year": [2012, 2016, 2020]})
# invoking to_json() to convert data frame to json
json = df.to_json()
# print json string on console
print(json)

Explanation

  • Line 2–3: We load the numpy and pandas libraries in the program.
  • Line 5–8: We convert a dictionary of three values into a pandas DataFrame.
  • Line 10: We transform the DataFrame content into JSON format using df.to_json().
  • Line 12: We print the JSON string to the console.

Free Resources