Skip to content

Commit 79b68a9

Browse files
committed
ENH: Support pd.json_normalize for normalizing only meta fields
1 parent a4fc97e commit 79b68a9

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

pandas/io/json/_normalize.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ def _pull_records(js: dict[str, Any], spec: list | str) -> list:
514514
):
515515
return DataFrame(_simple_json_normalize(data, sep=sep), index=index)
516516

517-
if record_path is None:
517+
if record_path is None and meta is None:
518518
if any([isinstance(x, dict) for x in y.values()] for y in data):
519519
# naive normalization, this is idempotent for flat records
520520
# and potentially will inflate the data considerably for
@@ -525,6 +525,8 @@ def _pull_records(js: dict[str, Any], spec: list | str) -> list:
525525
# reasonably
526526
data = nested_to_record(data, sep=sep, max_level=max_level)
527527
return DataFrame(data, index=index)
528+
elif record_path is None and meta is not None:
529+
record_path = []
528530
elif not isinstance(record_path, list):
529531
record_path = [record_path]
530532

@@ -554,23 +556,30 @@ def _recursive_extract(data, path, seen_meta, level: int = 0) -> None:
554556
_recursive_extract(obj[path[0]], path[1:], seen_meta, level=level + 1)
555557
else:
556558
for obj in data:
557-
recs = _pull_records(obj, path[0])
558-
recs = [
559-
nested_to_record(r, sep=sep, max_level=max_level)
560-
if isinstance(r, dict)
561-
else r
562-
for r in recs
563-
]
564-
565-
# For repeating the metadata later
566-
lengths.append(len(recs))
559+
if len(path) == 1:
560+
recs = _pull_records(obj, path[0])
561+
recs = [
562+
nested_to_record(r, sep=sep, max_level=max_level)
563+
if isinstance(r, dict)
564+
else r
565+
for r in recs
566+
]
567+
records.extend(recs)
568+
569+
# For repeating the metadata later
570+
lengths.append(len(recs))
571+
else:
572+
# If path is an empty list, data is treated as an
573+
# array of records, and only the meta fields will
574+
# be extract from each record.
575+
lengths.append(1)
576+
567577
for val, key in zip(_meta, meta_keys):
568578
if level + 1 > len(val):
569579
meta_val = seen_meta[key]
570580
else:
571581
meta_val = _pull_field(obj, val[level:])
572582
meta_vals[key].append(meta_val)
573-
records.extend(recs)
574583

575584
_recursive_extract(data, record_path, {}, level=0)
576585

0 commit comments

Comments
 (0)