Skip to content

Commit e7ac84d

Browse files
cswarthjorisvandenbossche
authored andcommitted
DOC: Expand on reference docs for read_json() (pandas-dev#14442)
1 parent 31ca717 commit e7ac84d

File tree

1 file changed

+69
-22
lines changed

1 file changed

+69
-22
lines changed

pandas/io/json.py

+69-22
Original file line numberDiff line numberDiff line change
@@ -123,32 +123,38 @@ def read_json(path_or_buf=None, orient=None, typ='frame', dtype=True,
123123
file. For file URLs, a host is expected. For instance, a local file
124124
could be ``file://localhost/path/to/table.json``
125125
126-
orient
127-
128-
* `Series`
129-
126+
orient : string,
127+
Indication of expected JSON string format.
128+
Compatible JSON strings can be produced by ``to_json()`` with a
129+
corresponding orient value.
130+
The set of possible orients is:
131+
132+
- ``'split'`` : dict like
133+
``{index -> [index], columns -> [columns], data -> [values]}``
134+
- ``'records'`` : list like
135+
``[{column -> value}, ... , {column -> value}]``
136+
- ``'index'`` : dict like ``{index -> {column -> value}}``
137+
- ``'columns'`` : dict like ``{column -> {index -> value}}``
138+
- ``'values'`` : just the values array
139+
140+
The allowed and default values depend on the value
141+
of the `typ` parameter.
142+
143+
* when ``typ == 'series'``,
144+
145+
- allowed orients are ``{'split','records','index'}``
130146
- default is ``'index'``
131-
- allowed values are: ``{'split','records','index'}``
132147
- The Series index must be unique for orient ``'index'``.
133148
134-
* `DataFrame`
149+
* when ``typ == 'frame'``,
135150
151+
- allowed orients are ``{'split','records','index',
152+
'columns','values'}``
136153
- default is ``'columns'``
137-
- allowed values are: {'split','records','index','columns','values'}
138-
- The DataFrame index must be unique for orients 'index' and
139-
'columns'.
140-
- The DataFrame columns must be unique for orients 'index',
141-
'columns', and 'records'.
142-
143-
* The format of the JSON string
144-
145-
- split : dict like
146-
``{index -> [index], columns -> [columns], data -> [values]}``
147-
- records : list like
148-
``[{column -> value}, ... , {column -> value}]``
149-
- index : dict like ``{index -> {column -> value}}``
150-
- columns : dict like ``{column -> {index -> value}}``
151-
- values : just the values array
154+
- The DataFrame index must be unique for orients ``'index'`` and
155+
``'columns'``.
156+
- The DataFrame columns must be unique for orients ``'index'``,
157+
``'columns'``, and ``'records'``.
152158
153159
typ : type of object to recover (series or frame), default 'frame'
154160
dtype : boolean or dict, default True
@@ -197,7 +203,48 @@ def read_json(path_or_buf=None, orient=None, typ='frame', dtype=True,
197203
198204
Returns
199205
-------
200-
result : Series or DataFrame
206+
result : Series or DataFrame, depending on the value of `typ`.
207+
208+
See Also
209+
--------
210+
DataFrame.to_json
211+
212+
Examples
213+
--------
214+
215+
>>> df = pd.DataFrame([['a', 'b'], ['c', 'd']],
216+
... index=['row 1', 'row 2'],
217+
... columns=['col 1', 'col 2'])
218+
219+
Encoding/decoding a Dataframe using ``'split'`` formatted JSON:
220+
221+
>>> df.to_json(orient='split')
222+
'{"columns":["col 1","col 2"],
223+
"index":["row 1","row 2"],
224+
"data":[["a","b"],["c","d"]]}'
225+
>>> pd.read_json(_, orient='split')
226+
col 1 col 2
227+
row 1 a b
228+
row 2 c d
229+
230+
Encoding/decoding a Dataframe using ``'index'`` formatted JSON:
231+
232+
>>> df.to_json(orient='index')
233+
'{"row 1":{"col 1":"a","col 2":"b"},"row 2":{"col 1":"c","col 2":"d"}}'
234+
>>> pd.read_json(_, orient='index')
235+
col 1 col 2
236+
row 1 a b
237+
row 2 c d
238+
239+
Encoding/decoding a Dataframe using ``'records'`` formatted JSON.
240+
Note that index labels are not preserved with this encoding.
241+
242+
>>> df.to_json(orient='records')
243+
'[{"col 1":"a","col 2":"b"},{"col 1":"c","col 2":"d"}]'
244+
>>> pd.read_json(_, orient='records')
245+
col 1 col 2
246+
0 a b
247+
1 c d
201248
"""
202249

203250
filepath_or_buffer, _, _ = get_filepath_or_buffer(path_or_buf,

0 commit comments

Comments
 (0)