
- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
Python Pandas - Write JSON
The Pandas library in Python provides a simple and efficient way to convert Series and DataFrames into JSON strings or write them to JSON files using the to_json() method.
JSON stands for JavaScript Object Notation and is a lightweight, text-based format used for structured data exchange and storage. In this tutorial, we will explore the various advanced options available for exporting data to JSON format using Pandas.
The to_json() Method
The to_json() method allows exporting Pandas objects (Series or DataFrame) into JSON strings or files with more formatting options.
The syntax of this method is as follows −
object.to_json(path_or_buf=None, *, orient=None, date_format=None, lines=False, compression='infer', mode='w')
Below are the key parameters of the method −
path_or_buf: Specifies the file path or buffer where the JSON output will be written. If set to None, the method returns a JSON string.
orient: Specifies the format of the JSON string.
date_format: Specifies the format for encoding dates.
lines: If set to True with orient='records', writes each record on a new line.
mode: Specifies the file mode when writing to a file. Available options are 'w' (write), and 'a' (append).
Example
Let's convert a Pandas DataFrame into a JSON string using the to_json() method.
import pandas as pd # Create a DataFrame df = pd.DataFrame({"Name":["Kiran", "Dev", "Priya"], "Gender": ["Male", "Female", "Female"], "Age": [21, 25, 29]}) # Write DataFrame to a JSON file df.to_json("output_json_file.json") print("The output JSON file has been written successfully.")
On executing the above code we will get the following output −
The output JSON file has been written successfully.
After executing the above code, you can find the created JSON file named output_json_file.json in your working directory.
Control JSON Structure with Orient Parameter
The orient parameter in the to_json() method controls the structure of the JSON output. The main options include "split", "records", "index", "columns", "values", and "table". Each option determines how the JSON output is structured −
split: Splits data into columns, index, and data sections.
records: Each row is represented as a dictionary.
index: Keys are index, values are dictionaries.
columns: Keys are column names, values are dictionaries.
table: The JSON Table Schema.
Example
The following example demonstrates the use of the to_json() method with the different orientation options.
import pandas as pd # Create a DataFrame df = pd.DataFrame({"Name":["Kiran", "Dev", "Priya"], "Gender": ["Male", "Female", "Female"], "Age": [21, 25, 29]}) # Column-oriented JSON print("Column-oriented JSON") print(df.to_json(orient="columns")) # Record-oriented JSON print("\nRecord-oriented JSON") print(df.to_json(orient="records")) # Split-oriented JSON print("\nSplit-oriented JSON") print(df.to_json(orient="split"))
Following is an output of the above code −
Column-oriented JSON {"Name":{"0":"Kiran","1":"Dev","2":"Priya"},"Gender":{"0":"Male","1":"Female","2":"Female"},"Age":{"0":21,"1":25,"2":29}} Record-oriented JSON [{"Name":"Kiran","Gender":"Male","Age":21},{"Name":"Dev","Gender":"Female","Age":25},{"Name":"Priya","Gender":"Female","Age":29}] Split-oriented JSON {"columns":["Name","Gender","Age"],"index":[0,1,2],"data":[["Kiran","Male",21],["Dev","Female",25],["Priya","Female",29]]}
Handling Dates While Converting DataFrame to JSON
Pandas can serialize datetime objects into JSON with specific formats using the date_format and date_unit parameters.
Example
The following example demonstrates how to handle dates while converting Pandas DataFrame to JSON using the date_format and date_unit parameters of the to_json() method.
import pandas as pd # Create a DataFrame df = pd.DataFrame({ "A": [1, 2, 3], "date": pd.date_range("2025-01-01", periods=3) }) # Writing ISO format print('Output JSON with dates converted to ISO format:') print(df.to_json(date_format="iso")) # Writing Epoch format (milliseconds) print('\nOutput JSON with dates converted to Epoch format (milliseconds):') print(df.to_json(date_format="epoch", date_unit="ms"))
Upon executing the above code, we obtain the following output −
Output JSON with dates converted to ISO format: {"A":{"0":1,"1":2,"2":3},"date":{"0":"2025-01-01T00:00:00.000","1":"2025-01-02T00:00:00.000","2":"2025-01-03T00:00:00.000"}} Output JSON with dates converted to Epoch format (milliseconds): {"A":{"0":1,"1":2,"2":3},"date":{"0":1735689600000,"1":1735776000000,"2":1735862400000}}
Exporting DataFrame to JSON with Custom Indentation
The indent parameter in the to_json() method controls the amount of white-space used to format JSON output
Example
The following example demonstrates how to specify the custom indentation for the output JSON by using the The indent parameter of the to_json() method.
import pandas as pd # Create a DataFrame df = pd.DataFrame({"Name":["Kiran", "Dev", "Priya"], "Gender": ["Male", "Female", "Female"], "Age": [21, 25, 29]}) # Convert to JSON with Custom Indentation result = df.to_json(indent=5) # Print the resulting JSON string print("Output JSON String:") print(result)
When we run above program, it produces following result −
Output JSON String: { "Name":{ "0":"Kiran", "1":"Dev", "2":"Priya" }, "Gender":{ "0":"Male", "1":"Female", "2":"Female" }, "Age":{ "0":21, "1":25, "2":29 } }
Pandas Appending to Existing JSON File
Pandas also supports exporting Pandas object to an existing JSON file using the mode option of the to_json() method. This option is supported only when the lines parameter is set to True and orient is set to 'records'.
Example
The following example demonstrates how to append data to an existing JSON file.
import pandas as pd # Create a DataFrame df = pd.DataFrame({ "Name": ["Kiran", "Dev", "Priya"], "Gender": ["Male", "Female", "Female"], "Age": [21, 25, 29] }) # Convert to JSON with lines=True and orient="records" df.to_json("output.json", orient="records", lines=True) # Read the JSON data back into a DataFrame result = pd.read_json('output.json', lines=True) print('DataFrame from JSON File:') print(result) # Additional data df2 = pd.DataFrame({ "Name": ['Vinay'], "Gender": ['Male'], "Age": [40] }) # Append to the existing JSON file df2.to_json("output.json", orient="records", lines=True, mode="a") # Read the JSON data back into a DataFrame result_2 = pd.read_json('output.json', lines=True) print('\nDataFrame from JSON File after Appending:') print(result_2)
Following is an output of the above code −
DataFrame from JSON File:
Name | Gender | Age | |
---|---|---|---|
0 | Kiran | Male | 21 |
1 | Dev | Female | 25 |
2 | Priya | Female | 29 |
Name | Gender | Age | |
---|---|---|---|
0 | Kiran | Male | 21 |
1 | Dev | Female | 25 |
2 | Priya | Female | 29 |
3 | Vinay | Male | 40 |