Pandas: add indent support to `to_json` method

Created on 9 Jan 2016  Â·  9Comments  Â·  Source: pandas-dev/pandas

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.

IO JSON

Most helpful comment

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

All 9 comments

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

Is it still on the list to be implemented?

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

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

It looks like the JSON writer uses Pandas' own JSON writer:
https://github.com/pandas-dev/pandas/blob/011b79fbf73b45313b47c08b4be1fc07dcb99365/pandas/io/json/json.py#L7

It looks like eventually it calls objToJSON in C here:
https://github.com/pandas-dev/pandas/blob/011b79fbf73b45313b47c08b4be1fc07dcb99365/pandas/_libs/src/ujson/python/objToJSON.c#L2317-L2321

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

Yep that sounds right. FYI, I think ripping out that JSON code and
replacing it with something more maintainable
(maybe based on Apache Arrow's JSON support) is on the medium to long-term
agenda.

On Mon, Nov 12, 2018 at 9:28 AM Kyle Barron notifications@github.com
wrote:

It looks like the JSON writer
https://github.com/pandas-dev/pandas/blob/011b79fbf73b45313b47c08b4be1fc07dcb99365/pandas/io/json/json.py#L107-L117
uses Pandas' own JSON writer:

https://github.com/pandas-dev/pandas/blob/011b79fbf73b45313b47c08b4be1fc07dcb99365/pandas/io/json/json.py#L7

It looks like eventually it calls objToJSON in C here:

https://github.com/pandas-dev/pandas/blob/011b79fbf73b45313b47c08b4be1fc07dcb99365/pandas/_libs/src/ujson/python/objToJSON.c#L2317-L2321

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

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/pandas-dev/pandas/issues/12004#issuecomment-437922165,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABQHIiHGfDOPOU98wggIOgkOVEaLVrPgks5uuZOKgaJpZM4HBmld
.

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

Yep. It also handles types not supported by the stdlib's json writer
(notably numpy types).

On Mon, Nov 12, 2018 at 9:55 AM Kyle Barron notifications@github.com
wrote:

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

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/pandas-dev/pandas/issues/12004#issuecomment-437932241,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABQHIvmwtHI6ob3U9Tb4WZVI97EsrOd8ks5uuZn9gaJpZM4HBmld
.

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

Was this page helpful?
0 / 5 - 0 ratings