Pandas Series.to_json()


Pandas Series.to_json() Tutorial

In this tutorial, we'll explore the Series.to_json() method in Pandas, which is used to convert a Pandas Series into a JSON string, with the help of well detailed example programs.

The syntax of `Series.to_json() provides several parameters to customize the behavior:

Series.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,
    mode='w'
)

where

ParameterDescription
path_or_buf[Optional] The path or buffer where the JSON string is to be saved. If not specified, the result is returned as a string.
orient[Optional] The format of the JSON string. Options include 'split', 'records', 'index', 'columns', and 'values'.
date_format[Optional] The date format to use. Default is None.
double_precision[Optional] The number of decimal places to round to for floating-point values. Default is 10.
force_ascii[Optional] Whether to escape non-ASCII characters. Default is True.
date_unit[Optional] The time unit to encode time-like values. Default is 'ms' (milliseconds).
default_handler[Optional] The function to use for encoding. Default is None.
lines[Optional] Whether to format the output with line breaks. Default is False.
compression[Optional] Compression options for the output file. Default is 'infer'.
index[Optional] Whether to include the index in the JSON string. Default is True.
indentLength of whitespace used to indent each record.
storage_optionsExtra options that make sense for a particular storage connection.
modeSpecify the IO mode for output when supplying a path_or_buf.
The default value is 'w'.
Parameters of Series.to_json()

The Series.to_json() method converts a Pandas Series into a JSON string. The resulting JSON can be written to a file, stored in a variable, or sent over the network.

Examples for Series.to_json()

1. Convert Series to JSON String

In this example, we'll use Series.to_json() to convert a Pandas Series into a JSON string.

Python Program

import pandas as pd

# Create a Series
series = pd.Series({'Name': 'Alice', 'Age': 25, 'City': 'New York'})

# Convert the Series to a JSON string
json_string = series.to_json()

# Print the original Series and the resulting JSON string
print("Original Series:")
print(series)
print("\nResulting JSON String:")
print(json_string)

Output

Original Series:
Name       Alice
Age           25
City    New York
dtype: object

Resulting JSON String:
{"Name":"Alice","Age":25,"City":"New York"}

2. Convert Series to JSON String with Custom Orientation

In this example, we'll use Series.to_json() with a custom orientation to convert a Pandas Series into a JSON string.

Python Program

import pandas as pd

# Create a Series
series = pd.Series({'Name': 'Alice', 'Age': 25, 'City': 'New York'})

# Convert the Series to a JSON string with 'records' orientation
json_string = series.to_json(orient='records')

# Print the original Series and the resulting JSON string
print("Original Series:")
print(series)
print("\nResulting JSON String:")
print(json_string)

Output

Original Series:
Name       Alice
Age           25
City    New York
dtype: object

Resulting JSON String:
["Alice",25,"New York"]

3. Convert Series to JSON String without Index

In this example, we'll use Series.to_json() to convert a Pandas Series into a JSON string without including the index.

Python Program

import pandas as pd

# Create a Series
series = pd.Series({'Name': 'Alice', 'Age': 25, 'City': 'New York'})

# Convert the Series to a JSON string without including the index
json_string = series.to_json(index=False)

# Print the original Series and the resulting JSON string
print("Original Series:")
print(series)
print("\nResulting JSON String:")
print(json_string)

Output

Original Series:
Name       Alice
Age           25
City    New York
dtype: object

Resulting JSON String:
{"Name":"Alice","Age":25,"City":"New York"}

Summary

In this tutorial, we've covered the Series.to_json() method in Pandas, which allows you to convert a Pandas Series into a JSON string. The method provides various parameters to customize the output, including specifying the orientation, date format, and compression options.


Python Libraries