The to_json()
method in Pandas is used to convert a DataFrame to a JSON-formatted string or to write it to a JSON file.
Example
import pandas as pd
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'San Francisco', 'Los Angeles']
}
df = pd.DataFrame(data)
# write df to a json file
df.to_json('output.json')
'''
output.json
{"Name":{"0":"Alice","1":"Bob","2":"Charlie"},"Age":{"0":25,"1":30,"2":35},"City":{"0":"New York","1":"San Francisco","2":"Los Angeles"}}
'''
to_json() Syntax
The syntax of the to_json()
method in Pandas is:
df.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)
to_json() Arguments
The to_json()
method takes the following common arguments:
path_or_buf
(optional): the file path or object where the JSON will be savedorient
(optional): the format of the JSON stringdate_format
(optional): controls the formatting of date valuesdouble_precision
(optional): specifies the number of decimal places for floating-point numbersforce_ascii
(optional): forces ASCII encoding for stringsdate_unit
(optional): the time unit to encode for datetime valuesdefault_handler
(optional): function to be called for objects that can't otherwise be serializedlines
(optional): whether to write the file as a JSON object per linecompression
(optional): specifies the compression format for the output fileindex
(optional): include the index as part of the JSON string or fileindent
(optional): Sets the indentation level for pretty-printed JSON output
to_json() Return Value
The return value of to_json()
is either None
, when writing to a file, or the JSON-formatted string
representation of the DataFrame, when no file path is specified.
Example 1: Write to a JSON File
import pandas as pd
# create a DataFrame
data = {'Name': ['Tom', 'Nick', 'John'],
'Age': [20, 21, 19],
'City': ['New York', 'London', 'Paris']}
df = pd.DataFrame(data)
# write to JSON file
df.to_json(path_or_buf='output.json')
output.json
{"Name":{"0":"Tom","1":"Nick","2":"John"},"Age":{"0":20,"1":21,"2":19},"City":{"0":"New York","1":"London","2":"Paris"}}
In this example, we wrote df to the JSON file output.json
.
Example 2: Using Different Orientations in JSON
import pandas as pd
# create DataFrame
data = {'Name': ['Tom', 'Nick', 'John'],
'Age': [20, 21, 19],
'City': ['New York', 'London', 'Paris']}
df = pd.DataFrame(data)
# save DataFrame in different JSON orientations
df.to_json('output_columns.json', orient='columns')
df.to_json('output_records.json', orient='records')
output_columns.json
{"Name":{"0":"Tom","1":"Nick","2":"John"},"Age":{"0":20,"1":21,"2":19},"City":{"0":"New York","1":"London","2":"Paris"}}
output_records.json
[{"Name":"Tom","Age":20,"City":"New York"},{"Name":"Nick","Age":21,"City":"London"},{"Name":"John","Age":19,"City":"Paris"}]
In this example, we saved the DataFrame with different orientations: columns
which is the default, and records
which gives a list of records.
Notice the difference between the contents of the two JSON files.
Example 3: Writing JSON with Different Date Formats
import pandas as pd
from datetime import datetime
# create DataFrame
data = {'Name': ['Tom', 'Nick', 'John'],
'Birthdate': [datetime(1998, 5, 12), datetime(1997, 8, 15), datetime(1996, 12, 25)]}
df = pd.DataFrame(data)
# save DataFrame with different date formats
df.to_json('output_epoch.json', date_format='epoch')
df.to_json('output_iso.json', date_format='iso')
output_epoch.json
{"Name":{"0":"Tom","1":"Nick","2":"John"},"Birthdate":{"0":894931200000,"1":871603200000,"2":851472000000}}
output_iso.json
{"Name":{"0":"Tom","1":"Nick","2":"John"},"Birthdate":{"0":"1998-05-12T00:00:00.000","1":"1997-08-15T00:00:00.000","2":"1996-12-25T00:00:00.000"}}
In this example, we formatted date values in JSON output using different date_format
options.
Here,
epoch
: represents the number of seconds that have elapsed since January 1, 1970, at 00:00:00 UTCiso
: is an international standard for representing dates and times
Example 4: Pretty Print JSON Output
import pandas as pd # create DataFrame data = {'Name': ['Tom', 'Nick', 'John'], 'Age': [20, 21, 19]} df = pd.DataFrame(data)
# pretty print JSON to string json_str = df.to_json(indent=4)print(json_str)
Output
{ "Name":{ "0":"Tom", "1":"Nick", "2":"John" }, "Age":{ "0":20, "1":21, "2":19 } }
In this example, we used indent=4
for pretty-printing the JSON output.
Example 5: Writing JSON with Non-ASCII Characters
import pandas as pd
# create DataFrame with non-ASCII characters
data = {'Name': ['Tomás', 'Niño', 'Jöhn'],
'Age': [20, 21, 19]}
df = pd.DataFrame(data)
# cave DataFrame with non-ASCII characters
df.to_json('output_ascii.json')
df.to_json('output_non_ascii.json', force_ascii=False)
output_ascii.json
{"Name":{"0":"Tom\u00e1s","1":"Ni\u00f1o","2":"J\u00f6hn"},"Age":{"0":20,"1":21,"2":19}}
output_non_ascii.json
{"Name":{"0":"Tomás","1":"Niño","2":"Jöhn"},"Age":{"0":20,"1":21,"2":19}}
Here, we used force_ascii=False
to allow non-ASCII characters like ñ
, ö
etc. in the JSON file.