Unlock the Power of Pandas: Converting DataFrames to JSON

The Syntax of to_json()

The to_json() method in Pandas takes several optional arguments that allow you to customize the output:

  • path_or_buf: specify the file path or object where the JSON will be saved
  • orient: choose the format of the JSON string
  • date_format: control the formatting of date values
  • double_precision: set the number of decimal places for floating-point numbers
  • force_ascii: force ASCII encoding for strings
  • date_unit: select the time unit to encode for datetime values
  • default_handler: define a function to handle objects that can’t be serialized
  • lines: write the file as a JSON object per line
  • compression: specify the compression format for the output file
  • index: include the index as part of the JSON string or file
  • indent: set the indentation level for pretty-printed JSON output

Return Value: What to Expect

When using to_json(), you can expect one of two return values: either None when writing to a file, or the JSON-formatted string representation of the DataFrame when no file path is specified.

Practical Examples: Putting to_json() to Work

Writing to a JSON File

import pandas as pd

df = pd.DataFrame({'name': ['John', 'Mary'], 'age': [25, 31]})
df.to_json('output.json', orient='columns')

Exploring Different Orientations

df.to_json('output_columns.json', orient='columns')
df.to_json('output_records.json', orient='records')

Compare the contents of the two JSON files: output_columns.json and output_records.json.

Customizing Date Formats

df = pd.DataFrame({'date': [pd.Timestamp('2022-01-01')]})
df.to_json('output_epoch.json', date_format='epoch')
df.to_json('output_iso.json', date_format='iso')

Check out the results in output_epoch.json and output_iso.json.

Pretty Printing JSON Output

df.to_json('output_pretty.json', indent=4)

In this example, we’ll set indent=4 for a neatly formatted output.

Working with Non-ASCII Characters

df = pd.DataFrame({'name': ['ña', 'ö']})
df.to_json('output_ascii.json', force_ascii=True)
df.to_json('output_non_ascii.json', force_ascii=False)

Compare the results in output_ascii.json and output_non_ascii.json.

Leave a Reply