@@ -2182,46 +2182,141 @@ def to_json(
2182
2182
2183
2183
Examples
2184
2184
--------
2185
+ >>> import json
2185
2186
>>> df = pd.DataFrame(
2186
2187
... [["a", "b"], ["c", "d"]],
2187
2188
... index=["row 1", "row 2"],
2188
2189
... columns=["col 1", "col 2"],
2189
2190
... )
2190
2191
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
+ }
2195
2215
2196
2216
Encoding/decoding a Dataframe using ``'records'`` formatted JSON.
2197
2217
Note that index labels are not preserved with this encoding.
2198
2218
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
+ ]
2201
2232
2202
2233
Encoding/decoding a Dataframe using ``'index'`` formatted JSON:
2203
2234
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
+ }
2206
2248
2207
2249
Encoding/decoding a Dataframe using ``'columns'`` formatted JSON:
2208
2250
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
+ }
2211
2264
2212
2265
Encoding/decoding a Dataframe using ``'values'`` formatted JSON:
2213
2266
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
+ ]
2218
2280
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
+ }
2225
2320
"""
2226
2321
from pandas .io import json
2227
2322
0 commit comments