Skip to content

add indent support to to_json method #12004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
phretor opened this issue Jan 9, 2016 · 9 comments · Fixed by #28130
Closed

add indent support to to_json method #12004

phretor opened this issue Jan 9, 2016 · 9 comments · Fixed by #28130
Labels
IO JSON read_json, to_json, json_normalize

Comments

@phretor
Copy link

phretor commented Jan 9, 2016

Sometimes you just want to have a quick look at your data in IPython and having to_json to support indent (as both json.dumps and simplejson.dumps do) would be very handy.

A workaround, which however doesn't always work, is to do json.dumps(series.to_dict()), which unfortunately is annoying when you don't have a JSON-serializable type (e.g., numpy.bool_).

I'm leaving this here as a food for discussion.

@jorisvandenbossche jorisvandenbossche added the IO JSON read_json, to_json, json_normalize label Jan 9, 2016
@ludaavics
Copy link

Another workaround: df_json_pretty = json.dumps(json.loads(df.to_json()), indent=2)

@devforfu
Copy link

Is it still on the list to be implemented?

@TomAugspurger
Copy link
Contributor

Still open. Feel free to work on it if you want, but the current json isn't the friendliest.

@TomAugspurger TomAugspurger added this to the Contributions Welcome milestone Nov 12, 2018
@devforfu
Copy link

@TomAugspurger Ok, got it! I'll try to figure out if there is a straightforward way to deal with it.

@kylebarron
Copy link
Contributor

It looks like the JSON writer uses Pandas' own JSON writer:

import pandas._libs.json as json

It looks like eventually it calls objToJSON in C here:

PyObject *objToJSON(PyObject *self, PyObject *args, PyObject *kwargs) {
static char *kwlist[] = {
"obj", "ensure_ascii", "double_precision", "encode_html_chars",
"orient", "date_unit", "iso_dates", "default_handler",
NULL};

So if I'm not mistaken, to add an indent option, you'd need to change the C code?

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Nov 12, 2018 via email

@kylebarron
Copy link
Contributor

Is the current JSON writer believed to be much faster than the Python standard library's JSON writer?

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Nov 12, 2018 via email

@botenvouwer
Copy link

I agree on this, JSON is also meant to be human readable. Right now this is just annoying.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO JSON read_json, to_json, json_normalize
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants