Skip to content

Commit f3dd043

Browse files
author
MomIsBestFriend
committed
to_json examples are now pretty printed
1 parent 144caa8 commit f3dd043

File tree

1 file changed

+115
-20
lines changed

1 file changed

+115
-20
lines changed

pandas/core/generic.py

+115-20
Original file line numberDiff line numberDiff line change
@@ -2182,46 +2182,141 @@ def to_json(
21822182
21832183
Examples
21842184
--------
2185+
>>> import json
21852186
>>> df = pd.DataFrame(
21862187
... [["a", "b"], ["c", "d"]],
21872188
... index=["row 1", "row 2"],
21882189
... columns=["col 1", "col 2"],
21892190
... )
21902191
2191-
>>> df.to_json(orient='split')
2192-
'{"columns":["col 1","col 2"],\
2193-
"index":["row 1","row 2"],\
2194-
"data":[["a","b"],["c","d"]]}'
2192+
>>> result = df.to_json(orient="split")
2193+
>>> parsed = json.loads(result)
2194+
>>> json.dumps(parsed, indent=4) # doctest: +SKIP
2195+
{
2196+
"columns": [
2197+
"col 1",
2198+
"col 2"
2199+
],
2200+
"index": [
2201+
"row 1",
2202+
"row 2"
2203+
],
2204+
"data": [
2205+
[
2206+
"a",
2207+
"b"
2208+
],
2209+
[
2210+
"c",
2211+
"d"
2212+
]
2213+
]
2214+
}
21952215
21962216
Encoding/decoding a Dataframe using ``'records'`` formatted JSON.
21972217
Note that index labels are not preserved with this encoding.
21982218
2199-
>>> df.to_json(orient='records')
2200-
'[{"col 1":"a","col 2":"b"},{"col 1":"c","col 2":"d"}]'
2219+
>>> result = df.to_json(orient="records")
2220+
>>> parsed = json.loads(result)
2221+
>>> json.dumps(parsed, indent=4) # doctest: +SKIP
2222+
[
2223+
{
2224+
"col 1": "a",
2225+
"col 2": "b"
2226+
},
2227+
{
2228+
"col 1": "c",
2229+
"col 2": "d"
2230+
}
2231+
]
22012232
22022233
Encoding/decoding a Dataframe using ``'index'`` formatted JSON:
22032234
2204-
>>> df.to_json(orient='index')
2205-
'{"row 1":{"col 1":"a","col 2":"b"},"row 2":{"col 1":"c","col 2":"d"}}'
2235+
>>> result = df.to_json(orient="index")
2236+
>>> parsed = json.loads(result)
2237+
>>> json.dumps(parsed, indent=4) # doctest: +SKIP
2238+
{
2239+
"row 1": {
2240+
"col 1": "a",
2241+
"col 2": "b"
2242+
},
2243+
"row 2": {
2244+
"col 1": "c",
2245+
"col 2": "d"
2246+
}
2247+
}
22062248
22072249
Encoding/decoding a Dataframe using ``'columns'`` formatted JSON:
22082250
2209-
>>> df.to_json(orient='columns')
2210-
'{"col 1":{"row 1":"a","row 2":"c"},"col 2":{"row 1":"b","row 2":"d"}}'
2251+
>>> result = df.to_json(orient="columns")
2252+
>>> parsed = json.loads(result)
2253+
>>> json.dumps(parsed, indent=4) # doctest: +SKIP
2254+
{
2255+
"col 1": {
2256+
"row 1": "a",
2257+
"row 2": "c"
2258+
},
2259+
"col 2": {
2260+
"row 1": "b",
2261+
"row 2": "d"
2262+
}
2263+
}
22112264
22122265
Encoding/decoding a Dataframe using ``'values'`` formatted JSON:
22132266
2214-
>>> df.to_json(orient='values')
2215-
'[["a","b"],["c","d"]]'
2216-
2217-
Encoding with Table Schema
2267+
>>> result = df.to_json(orient="values")
2268+
>>> parsed = json.loads(result)
2269+
>>> json.dumps(parsed, indent=4) # doctest: +SKIP
2270+
[
2271+
[
2272+
"a",
2273+
"b"
2274+
],
2275+
[
2276+
"c",
2277+
"d"
2278+
]
2279+
]
22182280
2219-
>>> df.to_json(orient='table')
2220-
'{"schema":{"fields":[{"name":"index","type":"string"},\
2221-
{"name":"col 1","type":"string"},{"name":"col 2","type":"string"}],\
2222-
"primaryKey":["index"],"pandas_version":"0.20.0"},\
2223-
"data":[{"index":"row 1","col 1":"a","col 2":"b"},\
2224-
{"index":"row 2","col 1":"c","col 2":"d"}]}'
2281+
Encoding with Table Schema:
2282+
2283+
>>> result = df.to_json(orient="table")
2284+
>>> parsed = json.loads(result)
2285+
>>> json.dumps(parsed, indent=4) # doctest: +SKIP
2286+
{
2287+
"schema": {
2288+
"fields": [
2289+
{
2290+
"name": "index",
2291+
"type": "string"
2292+
},
2293+
{
2294+
"name": "col 1",
2295+
"type": "string"
2296+
},
2297+
{
2298+
"name": "col 2",
2299+
"type": "string"
2300+
}
2301+
],
2302+
"primaryKey": [
2303+
"index"
2304+
],
2305+
"pandas_version": "0.20.0"
2306+
},
2307+
"data": [
2308+
{
2309+
"index": "row 1",
2310+
"col 1": "a",
2311+
"col 2": "b"
2312+
},
2313+
{
2314+
"index": "row 2",
2315+
"col 1": "c",
2316+
"col 2": "d"
2317+
}
2318+
]
2319+
}
22252320
"""
22262321
from pandas.io import json
22272322

0 commit comments

Comments
 (0)