• Post author:
  • Post category:Pandas
  • Post last modified:December 8, 2024
  • Reading time:19 mins read
You are currently viewing Pandas Convert Series to Json

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.

Advertisements

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 keys index, data, and columns.
    • '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.

pandas convert series json

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.
pandas convert series json

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}].

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.

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

How do I convert a Pandas Series to a JSON string?

To convert a Pandas Series to a JSON string, you can use the .to_json() method.

What is the default format of the JSON when converting a Series?

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.

Can I include the index in the JSON output?

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.

How do I handle missing values when converting to JSON?

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.

How can I pretty-print 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 !!

References