@@ -221,6 +221,7 @@ def from_dict(as_dict: dict,
221
221
222
222
def from_json (as_json : Union [str , 'PathLike[Any]' , BytesIO ],
223
223
target : Optional [Union ['PathLike[Any]' , BytesIO ]] = None ,
224
+ layout : str = 'records' ,
224
225
compress : bool = False ,
225
226
** kwargs ):
226
227
"""Convert JSON data into an SPSS dataset.
@@ -242,12 +243,33 @@ def from_json(as_json: Union[str, 'PathLike[Any]', BytesIO],
242
243
:type target: Path-like / :class:`BytesIO <python:io.BytesIO>` /
243
244
:obj:`None <python:None>`
244
245
246
+ :param layout: Indicates the layout schema to use for the JSON representation of the
247
+ data. Accepts:
248
+
249
+ * ``records``, where the resulting JSON object represents an array of objects
250
+ where each object corresponds to a single record, with key/value pairs for each
251
+ column and that record's corresponding value
252
+ * ``table``, where the resulting JSON object contains a metadata (data map)
253
+ describing the data schema along with the resulting collection of record objects
254
+
255
+ Defaults to ``records``.
256
+
257
+ :type layout: :class:`str <python:str>`
258
+
245
259
:param compress: If ``True``, will return data in the compressed ZSAV format. If
246
260
``False``, will return data in the standards SAV format. Defaults to ``False``.
247
261
:type compress: :class:`bool <python:bool>`
248
262
249
263
:param kwargs: Additional keyword arguments which will be passed onto the
250
264
:func:`pandas.read_json() <pandas:pandas.read_json>` function.
265
+
266
+ .. warning::
267
+
268
+ If you supply an ``orient`` keyword argument (which is supported by
269
+ :func:`pandas.read_json() <pandas:pandas.read_json>`), the ``orient``
270
+ value will *override* the value supplied for ``layout``. This is an
271
+ advanced use case, so use with caution.
272
+
251
273
:type kwargs: :class:`dict <python:dict>`
252
274
253
275
:returns: A :class:`BytesIO <python:io.BytesIO>` object containing the SPSS data if
@@ -256,7 +278,16 @@ def from_json(as_json: Union[str, 'PathLike[Any]', BytesIO],
256
278
:rtype: :class:`BytesIO <python:io.BytesIO>` or :obj:`None <python:None>`
257
279
258
280
"""
259
- df = pandas .read_json (as_json , ** kwargs )
281
+ if layout not in ['records' , 'table' ]:
282
+ raise errors .InvalidLayoutError ('layout must be either "records" or "table". '
283
+ f'Was: "{ layout } "' )
284
+
285
+ orient = kwargs .pop ('orient' , layout )
286
+
287
+ df = pandas .read_json (as_json ,
288
+ orient = orient ,
289
+ ** kwargs )
290
+
260
291
result = from_dataframe (df ,
261
292
target = target ,
262
293
compress = compress )
@@ -266,6 +297,7 @@ def from_json(as_json: Union[str, 'PathLike[Any]', BytesIO],
266
297
267
298
def from_yaml (as_yaml : Union [str , 'PathLike[Any]' , BytesIO ],
268
299
target : Optional [Union ['PathLike[Any]' , BytesIO ]] = None ,
300
+ layout : str = 'records' ,
269
301
compress : bool = False ,
270
302
** kwargs ):
271
303
"""Convert YAML data into an SPSS dataset.
@@ -287,12 +319,35 @@ def from_yaml(as_yaml: Union[str, 'PathLike[Any]', BytesIO],
287
319
:type target: Path-like / :class:`BytesIO <python:io.BytesIO>` /
288
320
:obj:`None <python:None>`
289
321
322
+ :param layout: Indicates the layout schema to expect for the YAML representation of the
323
+ data. Accepts:
324
+
325
+ * ``records``, where the resulting JSON object represents an array of objects
326
+ where each object corresponds to a single record, with key/value pairs for each
327
+ column and that record's corresponding value
328
+ * ``table``, where the resulting JSON object contains a metadata (data map)
329
+ describing the data schema along with the resulting collection of record objects
330
+
331
+ Defaults to ``records``.
332
+ :type layout: :class:`str <python:str>`
333
+
290
334
:param compress: If ``True``, will return data in the compressed ZSAV format. If
291
335
``False``, will return data in the standards SAV format. Defaults to ``False``.
292
336
:type compress: :class:`bool <python:bool>`
293
337
294
338
:param kwargs: Additional keyword arguments which will be passed onto the
295
- :meth:`DataFrame.from_dict() <pandas:pandas.DataFrame.from_dict>` method.
339
+ :meth:`pandas.from_json() <pandas:pandas.from_json>` function.
340
+
341
+ :param kwargs: Additional keyword arguments which will be passed onto the
342
+ :func:`pandas.read_json() <pandas:pandas.read_json>` function.
343
+
344
+ .. warning::
345
+
346
+ If you supply an ``orient`` keyword argument (which is supported by
347
+ :func:`pandas.read_json() <pandas:pandas.read_json>`), the ``orient``
348
+ value will *override* the value supplied for ``layout``. This is an
349
+ advanced use case, so use with caution.
350
+
296
351
:type kwargs: :class:`dict <python:dict>`
297
352
298
353
:returns: A :class:`BytesIO <python:io.BytesIO>` object containing the SPSS data if
@@ -312,6 +367,7 @@ def from_yaml(as_yaml: Union[str, 'PathLike[Any]', BytesIO],
312
367
313
368
return from_json (as_json ,
314
369
target = target ,
370
+ layout = layout ,
315
371
compress = compress ,
316
372
** kwargs )
317
373
0 commit comments