Skip to content

Commit 5793242

Browse files
committed
BUG: Fix json_normalize meta validation GH#63019
1 parent 2a10e04 commit 5793242

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

pandas/io/json/_normalize.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,28 @@ def _simple_json_normalize(
266266
return normalised_json_object
267267

268268

269+
def _validate_meta(meta: list) -> None:
270+
"""
271+
Validate that meta parameter contains only strings.
272+
273+
Parameters
274+
----------
275+
meta : list
276+
The meta parameter to validate.
277+
278+
Raises
279+
------
280+
TypeError
281+
If meta contains non-string elements.
282+
"""
283+
for item in meta:
284+
if not isinstance(item, str):
285+
raise TypeError(
286+
"All elements in 'meta' must be strings. "
287+
f"Found {type(item).__name__}: {item!r}"
288+
)
289+
290+
269291
def json_normalize(
270292
data: dict | list[dict] | Series,
271293
record_path: str | list | None = None,
@@ -426,6 +448,9 @@ def json_normalize(
426448
427449
Returns normalized data with columns prefixed with the given string.
428450
"""
451+
if meta is not None:
452+
_validate_meta(meta)
453+
429454

430455
def _pull_field(
431456
js: dict[str, Any], spec: list | str, extract_record: bool = False

pandas/tests/io/json/test_normalize.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,3 +903,21 @@ def test_series_non_zero_index(self):
903903
index=[1, 2, 3],
904904
)
905905
tm.assert_frame_equal(result, expected)
906+
907+
def test_json_normalize_meta_string_validation(self):
908+
# GH 63019
909+
data = [{"a": 1, 12: "meta_value", "nested": [{"b": 2}]}]
910+
911+
# Test non-string meta raises TypeError consistently
912+
with pytest.raises(TypeError, match="must be strings"):
913+
json_normalize(data, meta=[12])
914+
915+
with pytest.raises(TypeError, match="must be strings"):
916+
json_normalize(data, record_path=["nested"], meta=[12])
917+
918+
# Test string meta works correctly
919+
result1 = json_normalize(data, meta=["a"])
920+
assert "a" in result1.columns
921+
922+
result2 = json_normalize(data, record_path=["nested"], meta=["a"])
923+
assert "a" in result2.columns

0 commit comments

Comments
 (0)