Skip to content

to_json should make separators configurable (similar to json.dump) #33014

@bersbersbers

Description

@bersbersbers

Code Sample, a copy-pastable example if possible

import numpy as np
import pandas as pd

print(pd.DataFrame([1]).to_json(indent=4))

outputs

{
    "0":{
        "0":1
    }
}

Problem description

Any JSON prettyfier (including bare VS Code) wants to replace all : by : . So output of to_json should behave as json.dump does, at least when indent is not None (in which case the user clearly does not care about file size):

If specified, separators should be an (item_separator, key_separator) tuple.
The default is (', ', ': ') if indent is None and (',', ': ') otherwise.
To get the most compact JSON representation, you should specify (',', ':') to eliminate whitespace.

https://docs.python.org/3/library/json.html#:~:text=To%20get%20the%20most%20compact%20JSON%20representation

Expected Output

{
    "0": {
        "0": 1
    }
}

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.8.2.final.0
python-bits : 64
OS : Linux
OS-release : 4.12.14-lp151.28.32-default
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.3
numpy : 1.18.2
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.1.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 1.2.8
lxml.etree : 4.5.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : 4.8.0
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.5.0
matplotlib : 3.2.1
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.3
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
xlsxwriter : 1.2.8
numba : None

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions