@@ -514,7 +514,7 @@ def _pull_records(js: dict[str, Any], spec: list | str) -> list:
514
514
):
515
515
return DataFrame (_simple_json_normalize (data , sep = sep ), index = index )
516
516
517
- if record_path is None :
517
+ if record_path is None and meta is None :
518
518
if any ([isinstance (x , dict ) for x in y .values ()] for y in data ):
519
519
# naive normalization, this is idempotent for flat records
520
520
# and potentially will inflate the data considerably for
@@ -525,6 +525,8 @@ def _pull_records(js: dict[str, Any], spec: list | str) -> list:
525
525
# reasonably
526
526
data = nested_to_record (data , sep = sep , max_level = max_level )
527
527
return DataFrame (data , index = index )
528
+ elif record_path is None and meta is not None :
529
+ record_path = []
528
530
elif not isinstance (record_path , list ):
529
531
record_path = [record_path ]
530
532
@@ -554,23 +556,30 @@ def _recursive_extract(data, path, seen_meta, level: int = 0) -> None:
554
556
_recursive_extract (obj [path [0 ]], path [1 :], seen_meta , level = level + 1 )
555
557
else :
556
558
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
+
567
577
for val , key in zip (_meta , meta_keys ):
568
578
if level + 1 > len (val ):
569
579
meta_val = seen_meta [key ]
570
580
else :
571
581
meta_val = _pull_field (obj , val [level :])
572
582
meta_vals [key ].append (meta_val )
573
- records .extend (recs )
574
583
575
584
_recursive_extract (data , record_path , {}, level = 0 )
576
585
0 commit comments