How to Convert Python DataFrame to JSON

In this Python tutorial, we will learn how to convert Python DataFrame to JSON file. Also, we will cover these topics.

  • Python DataFrame to JSON
  • Python DataFrame to JSON Object
  • Python DataFrame to JSON Without Index
  • Python DataFrame to JSON Array
  • Python DataFrame to JSON String
  • Python DataFrame to JSON Format
  • Python DataFrame to JSON List
  • Python DataFrame to_json Orient
  • Python DataFrame to JSON with Index

Python DataFrame to JSON

In this section, we will learn how to convert Python DataFrame to JSON files. Pandas DataFrame can be converted to JSON files using dataframe.to_json() method.

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 for dataframe.to_json() method in Python Pandas

ParameterDescription
path_or_bufProvide the file path and filename where you want to save the file.
orient– It accepts a string and you can select from the given options
– for Series, there are 4 options are {‘split’, ‘records’, ‘index’, ‘table’}
– out of these options, the ‘index’ is the default.
– for DataFrame, options are 6 options {‘split’, ‘records’, ‘index’, ‘columns’, ‘values’, ‘table’}
– out of these options, ‘columns’ is the default.
date_formatType of date conversion. Available options are {None, ‘epoch’, ‘iso’}
– epoch’ = epoch milliseconds, ‘iso’ = ISO8601.
– The default depends on the orient.
– For orient=’table’, the default is ‘iso’.
– For all other orients, the default is ‘epoch’.
double_precisionaccepts int, default is 10. The number of decimal places to use when encoding floating-point values.
force_asciiaccepts boolean, default is True. Force encoded string to be ASCII.
date_unit– default is ‘ms’ (milliseconds)
– The time unit to encode governs timestamp and ISO8601 precision. One of ‘s’, ‘ms’, ‘us’, ‘ns’ for a second, millisecond, microsecond, and nanosecond respectively.
default_handler– Default value is None
– Handler to call if an object cannot otherwise be converted to a suitable format for JSON. Should receive a single argument which is the object to convert and return a serialisable object.
lines– accepts boolean, default is False
– If ‘orient’ is ‘records’ write out line-delimited JSON format. – It will throw ValueError if incorrect ‘orient’ since others are not list-like.
compression– available options are {‘infer’, ‘gzip’, ‘bz2’, ‘zip’, ‘xz’, None}
– A string representing the compression to use in the output file, only used when the first argument is a filename. By default, the compression is inferred from the filename.
index– accepts boolean, default is True
– Set index=False if you don’t want to include the index value in the JSON string.
– Please note that index=False is only supported when orient is ‘split’ or ‘table’
indent– accepts an integer value
– Length of whitespace used to indent each record.
storage_options– accepts dictionary
– this option is used to store connections for the host, port, username, password, etc.
– This option is mainly used with cloud service providers like AWS S3. The key-value pairs are forwarded to sffpec.
Parameters of df.to_json() method
  • In our implementation on Jupyter Notebook, we have demonstrated the use of necessary parameters.
  • Although, we have showed the use of almost all the parameters but only path_or_buf and orient are the required one rest all are optional to use.
  • Here is the implementation on Jupyter Notebook please read the inline comments to understand each step.

Read How to convert an integer to string in python

Python DataFrame to JSON Object

In this section, we will learn about Python DataFrame to JSON Object. We will create a JSON object and will display the JSON data.

  • There are two ways of converting python pandas dataframe to json object. First one is explained in previous section.
  • Other way is by using JSON module in Python.
  • In this method, we store the conversion in a variable instead of creating a file. In other words, we don’t require path_or_buf.
  • Here is the implementation on Jupyter Notebook. Please read in-line comments to understand each step.

Read How to convert floats to integer in Pandas

Python DataFrame to JSON Without Index

In this section, we will learn how to convert Python DataFrame to JSON without Index.

  • When we convert dataframe to json then an index is automatically added.
  • This Index creation can be ignored by setting index=False.
  • Index can be set to False only if the orient is ‘split’ or ‘table’.
  • Here is the implementation of Python DataFrame to JSON without Index on Jupyter Notebook.

Read Python convert binary to decimal

Python DataFrame to JSON Array

In this section, we will learn how to convert Python DataFrame to JSON Array.

  • Array is the collection of data in some format. In this case the format is JSON.
  • If you have read earlier topics then please don’t get confused with the word ‘Array’ here. It’s all have same procedure.
  • In this case, we will set the orient to index and index will be set to True.
  • Here is the implementation on Jupyter Notebook.

Read How to Convert Pandas DataFrame to NumPy Array in Python

Python DataFrame to JSON String

In this section, we will learn how to convert Python DataFrame to JSON String.

  • All the json files have string (‘str) type.
  • Dictionaries in python look similar to JSON files. They have ‘dict’ type.
  • If you ever get confused between these two then use type() method in python.
  • In our example, we have demonstrated Python Dataframe to JSON String.

Read Python convert DataFrame to list

Python DataFrame to JSON Format

In this section, we will learn how to convert Python DataFrame to JSON Format.

  • There are mainly two ways of converting python dataframe to json format.
    • First is by creating json object
    • second is by creating a json file
  • Json object holds the information till the time program is running and uses json module in python.
  • JSON file once created can be used outside of the program.
  • Here is the implementation on both the methods in jupyter notebook.

Read How to Convert Pandas DataFrame to a Dictionary

Python DataFrame to JSON List

In this section, we will learn how to convert Python DataFrame to JSON List.

  • List is used to store multiple items irrespective of their data types.
  • Dictionaries in Python look similar to JSON files but they are different and cannot be treating like json files. They have type as ‘dict’ whereas type of json file is ‘str’.
  • dataframe.to_dict() method has ‘list’ as orient option but dataframe.to_json() don’t have ‘list’ orient.
  • So first we will convert the dataframe to dictionary list and then convert it to JSON file.
  • json module provides json.dumps() method using which we can convert dictionary to json file.
  • In our implementation on Jupyter Notebook we have demonstrated each step with explanation.

Read Python convert dictionary to an array

Python DataFrame to_json Orient

In this section, we will learn about the Python DataFrame to_json Orient parameter.

  • Orient in dataframe.to_json() method determines how the json file will look like.
  • There are 4 types of orient in Series and 6 types in a DataFrame.
  • Here is the brief description of all 6 types of orients in python dataframe.to_json() method.:
    • split’ : dict like {‘index’ -> [index], ‘columns’ -> [columns], ‘data’ -> [values]}
    • records’ : list like [{column -> value}, … , {column -> value}]
    • index’ : dict like {index -> {column -> value}}
    • columns’ : dict like {column -> {index -> value}}
    • values’ : just the values array
    • table’ : dict like {‘schema’: {schema}, ‘data’: {data}}
  • Here is the implementation of each orient type on Jupyter Notebook.

Read Python convert dictionary to list

Python DataFrame to JSON with Index

In this section, will learn about Python DataFrame to JSON with Index.

  • Index is the column that keeps of record of each row. Last row of index is the total count of rows.
  • Index parameter is by default True for all the orients.
  • So doesn’t matter whatever orient you select index will be added automatically.
  • Alternative method is by adding index column using assign() method while converting dataframe to JSON file.
  • Here is the implementation of both methods of adding index to json file on jupyter notebook.

You may also like the following Python tutorials:

In this tutorial, we have learned how to convert Python DataFrame to JSON file. Also, we have covered these topics.

  • Python DataFrame to JSON
  • Python DataFrame to JSON Object
  • Python DataFrame to JSON Without Index
  • Python DataFrame to JSON Array
  • Python DataFrame to JSON String
  • Python DataFrame to JSON Format
  • Python DataFrame to JSON List
  • Python DataFrame to_json Orient
  • Python DataFrame to JSON with Index