To convert a Pandas Series to JSON, you can use the series.to_json()
method, which efficiently converts the Series into a JSON string. This method includes the key parameter orient
, which accepts values such as 'index'
, ‘records’
, ‘split’
, ‘table’
, and ‘values’
. This functionality is especially useful for data exchange between different platforms or for saving data in a human-readable format.
In this article, I will explain how to convert a Pandas Series into a JSON string. The Pandas Series.to_json()
method returns a JSON-formatted string that represents the data in the Series, with the structure determined by the chosen orient
and other parameters.
Key Points –
- The primary method to convert a Pandas Series to JSON format.
- The default orientation for
to_json()
is the ‘index’ format, where the Series index becomes the keys and values are the corresponding Series values. - Controls the structure of the JSON output, with options like ‘index’, ‘split’, ‘records’, ‘values’, and ‘table’.
to_json()
is optimized for quick serialization, making it suitable for large Series.- Series with a MultiIndex can be converted to JSON, but the format may require special handling for nested structures.
- Allows customization of how dates are represented, with options like ‘epoch’ or ‘iso’.
Syntax of Pandas Series.to_json()
Following is the syntax of the create Series.json() function.
# Syntax of pandas Series.to_json()
Series.to_json(path_or_buffer=None, orient='index', date_format=None, double_precision=10, force_ascii=True, date_unit='ms', default_handler=None)
Parameter of series.to_json()
Following are the parameters of the series.to_json()
path_or_buffer
– Optional. A string path, or a file-like object. If not specified, the JSON string is returned instead of being written to a file.orient
– Optional. Defines the format of the JSON string. Possible values are.'index'
(default): Series index becomes the keys.'split'
: A dict with keysindex
,data
, andcolumns
.'records'
: A list of dictionaries.'values'
: A JSON array of Series values.'table'
: A DataFrame-compatible schema.
date_format
– Optional. Specifies how dates are formatted.double_precision
– Optional. Controls the precision of floating-point numbers in the JSON string (default is 10).force_ascii
– Optional. If True (default), forces ASCII encoding; if False, allows non-ASCII characters in the output.date_unit
– Optional. Specifies the unit for date conversion (default is ‘ms’ for milliseconds).default_handler
– Optional. A function used to handle non-serializable objects in the Series.
Return Value
It returns a JSON string or a file-like object containing the Series data in the specified JSON format.
Usage of Pandas Convert Series to Json
Series.to_json()
is a method in Pandas that converts a Pandas Series to a JSON-formatted string. It allows for serializing a Series object into different JSON structures, such as key-value pairs, arrays, or tables, depending on the orient
parameter.
First, let’s create a Pandas Series from a list.
import pandas as pd
# Create the Series with a custom index
series = pd.Series([20000, 25000, 30000, 28000, 23000],
index=['Java', 'Spark', 'PySpark', 'Pandas', 'Python'])
print("Original Series:\n",series)
Yields below output.
To convert a Pandas Series to JSON with orient='index'
, you can use the to_json()
method with the orient
parameter explicitly set to 'index'
. This format treats the Series index as the keys and the Series values as the corresponding values in the JSON output.
# Convert the Series to JSON with orient='index'
ser = series.to_json(orient='index')
print("After convert the Series to JSON:\n",ser)
Here,
orient='index'
: This orientation makes the JSON output look like a dictionary where the Series index labels (‘Java’, ‘Spark’, etc.) are the keys, and the corresponding values (20000, 25000, etc.) are the JSON values.- The output is a JSON object with key-value pairs representing each item in the Series.
Convert Series to JSON Using orient=’split’
To convert a Pandas Series to JSON using orient='split'
, you can set the orient
parameter to 'split'
in the to_json()
method. This format divides the Series into a JSON object with separate keys for the index
and data
.
# Convert the Series to JSON with orient='split'
ser = series.to_json(orient='split')
print("After converting series to JSON string:\n",ser)
# Output:
# After converting series to JSON string:
# {"name":null,"index":["Java","Spark","PySpark","Pandas","Python"],"data":[20000,25000,30000,28000,23000]}
Here,
orient='split'
: This orientation outputs the JSON in a structure with two keys:"index"
: A list of the Series index labels (e.g.,["Java", "Spark", "PySpark", "Pandas", "Python"]
)."data"
: A list of the Series values (e.g.,[20000, 25000, 30000, 28000, 23000]
).
Using orient=’records’
To convert a Pandas Series to JSON using orient='records'
, set the orient
parameter to 'records'
in the to_json()
method. This format converts the Series into a list of dictionaries, where each dictionary represents an item in the Series.
# Convert the Series to JSON with orient='records'
ser = series.to_json(orient='records')
print("After converting series to JSON string:\n",ser)
# Output:
# After converting series to JSON string:
# [{"Java":20000},{"Spark":25000},{"PySpark":30000},{"Pandas":28000},{"Python":23000}]
Here,
orient='records'
: This orientation formats the JSON output as a list of dictionaries.- Each dictionary contains a single key-value pair where the key is the Series index label (e.g.,
"Java"
) and the value is the corresponding data (e.g.,20000
). - The JSON output is a list:
[{"Java":20000},{"Spark":25000},{"PySpark":30000},{"Pandas":28000},{"Python":23000}]
.
- Each dictionary contains a single key-value pair where the key is the Series index label (e.g.,
Using orient=’values’
To convert a Pandas Series to JSON using orient='values'
, set the orient
parameter to 'values'
in the to_json()
method. This format outputs the Series values as a simple JSON array, without including the index labels.
# Convert the Series to JSON with orient='values'
ser = series.to_json(orient='values')
print("After converting series to JSON string:\n",ser)
# Output:
# After converting series to JSON string:
# [20000,25000,30000,28000,23000]
Here,
orient='values'
: This orientation returns a JSON array containing only the values of the Series.- The Series index is not included in the output, resulting in a simple list of values:
[20000, 25000, 30000, 28000, 23000]
. - It’s the most compact format when only the data values are required, without any context of the index.
- The Series index is not included in the output, resulting in a simple list of values:
Using orient = ‘table’
To convert a Pandas Series to JSON using orient='table'
, set the orient
parameter to 'table'
in the to_json()
method. This format is structured to represent the Series in a tabular format, which is compatible with the JSON Table Schema. It includes both the index and the values in a structured way.
# Convert the Series to JSON with orient='table'
ser = series.to_json(orient='table')
print("After converting series to JSON string:\n",ser)
# Output:
# After converting series to JSON string:
{"schema":{"fields":[{"name":"index","type":"string"},{"name":"values","type":"integer"}],"primaryKey":["index"],"pandas_version":"0.20.0"},"data":[{"index":"Java","values":20000},{"index":"Spark","values":25000},{"index":"PySpark","values":30000},{"index":"Pandas","values":28000},{"index":"Python","values":23000}]}
Here,
orient='table'
: This format organizes the Series into a JSON Table Schema:"
schema
"
: Describes the fields (columns) and their types. In this case, there’s an"index"
field (type"string"
) and a"data"
field (type"integer"
)."data"
: Contains a list of records, where each record includes the index and corresponding value from the Series.
FAQ on Pandas Convert Series to Json
To convert a Pandas Series to a JSON string, you can use the .to_json()
method.
By default, the .to_json()
method converts the series into a dictionary where the index becomes the keys and the values are the data in the series.
By default, the index is included in the JSON output when you convert a Pandas Series to JSON using the .to_json()
method. The index is used as the key, and the series values are the corresponding values in the JSON object.
You can handle missing values by specifying the date_format
or default_handler
parameter in .to_json()
. If you have NaN or None values in your series, they will be converted to null
in the JSON output.
To pretty-print the JSON output after converting a Pandas Series to JSON, you can use Python’s built-in json
module.
Conclusion
In this article, you have learned how to convert a Pandas Series to JSON using the series.to_json()
method, along with various examples. For more params use to_json()
method from the pandas reference.
Happy Learning !!
Related Articles
- Pandas Series count() Function
- Pandas Series mode() Function
- Pandas Series.clip() Function
- Pandas Series iloc[] Function
- Pandas series.str.get() Function
- Pandas Series round() Function
- Pandas Series.dtype() Function
- Pandas Series.shift() Function
- Pandas Series any() Function
- Pandas Series rank() Function
- Pandas Series.rolling() Function
- Pandas Series.quantile() Function
- Pandas Series.str.contains() With Examples
- Pandas Series Drop duplicates() Function
- How to Make a Histogram in Pandas Series?
- Pandas Series.value_counts() With Examples