Skip to content

Commit 4fd3435

Browse files
committed
BUG: json_normalize handles non‑string keys in meta with record_path (GH#62264)
Coerce meta path elements to str when constructing meta_keys to avoid TypeError during sep.join and align with nested_to_record behavior. Add regression test in TestJSONNormalize to verify integer meta keys with record_path produce string-labeled columns and correct repetition. Closes GH#62264.
1 parent 83c9e4a commit 4fd3435

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

pandas/io/json/_normalize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ def _pull_records(js: dict[str, Any], spec: list | str) -> list:
540540
lengths = []
541541

542542
meta_vals: DefaultDict = defaultdict(list)
543-
meta_keys = [sep.join(val) for val in _meta]
543+
meta_keys = [sep.join(str(v) for v in val) for val in _meta]
544544

545545
def _recursive_extract(data, path, seen_meta, level: int = 0) -> None:
546546
if isinstance(data, dict):

pandas/tests/io/json/test_normalize.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,23 @@ def test_series_index(self, state_data):
569569
result = json_normalize(series, "counties")
570570
tm.assert_index_equal(result.index, idx.repeat([3, 2]))
571571

572+
def test_json_normalize_meta_int_key_with_record_path(self):
573+
# GH#62264
574+
data = [{"name": "Alice", 12: 20, "purchases": [{"pid": 301}, {"pid": 302}]}]
575+
576+
result = json_normalize(data, record_path=["purchases"], meta=[12, "name"])
577+
578+
expected = DataFrame(
579+
{
580+
"pid": [301, 302],
581+
"12": np.array([20, 20], dtype=object),
582+
"name": ["Alice", "Alice"],
583+
},
584+
columns=["pid", "12", "name"],
585+
)
586+
587+
tm.assert_frame_equal(result[["pid", "12", "name"]], expected)
588+
572589

573590
class TestNestedToRecord:
574591
def test_flat_stays_flat(self):

0 commit comments

Comments
 (0)