Skip to content

BUG: pd.json_normalize error when non-string keys are used in arguments #62264

@ybressler

Description

@ybressler

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
import pytest

sample_json = [
    {
        "customer_id": 1,
        12: 20,
        13: [{"a": [1, 2,3]}, {"a": [4, 5]}],
        14: [{1000: [1, 2,3]}, {1000: [4, 5]}],
        "name": "Alice",
        "purchases": [
            {
                "purchase_id": 301,
                "date": "2024-06-01",
                "waffles": [
                    {"waffle_id": "W1", "type": "Belgian", "flavor": "Vanilla", "quantity": 2},
                    {"waffle_id": "W2", "type": "Chocolate", "flavor": "Chocolate", "quantity": 1}
                ]
            },
            {
                "purchase_id": 302,
                "date": "2024-06-10",
                "waffles": [
                    {"waffle_id": "W3", "type": "Strawberry", "flavor": "Strawberry", "quantity": 3}
                ]
            }
        ]
    }
]

@pytest.mark.parametrize(
    "args",
    [
        pytest.param({}, id="no args (base case)"),
        pytest.param( dict(record_path=[13]), id="int as record path"),
        pytest.param( dict(record_path=[13, "a"]), id="int and str as record path"),
        pytest.param( dict(record_path=[14, 1_000]), id="int + int as record path"),
        pytest.param(dict(meta=[12]), id="int as meta field"),
        pytest.param(dict(meta=[12, 13]), id="int + int as meta field"),
        pytest.param(dict(meta=[12, "name"]), id="int + string as meta fields"),
        pytest.param(dict(meta=["name", 12]), id="int + string as meta fields (2)"),

        pytest.param(dict(record_path=["purchases"], meta=[12]), id="int as meta & record path"),
        pytest.param(dict(record_path=["purchases"], meta=[12, 13]), id="int +int as meta & record path"),
        pytest.param(dict(record_path=["purchases"], meta=[12, "name"]), id="int + string as meta fields & record path"),
        pytest.param(dict(record_path=["purchases"], meta=["name", 12]), id="int + string as meta fields & record path(2)"),

        pytest.param(

    ]
)
def test_normalize_df(args: dict, data: dict = sample_json):
    """"
    Load from nested object into a dataframe
    """
    df = pd.json_normalize(data, **args)

Issue Description

Specifying an integer as a meta field while specifying a record_path results in TypeError.

        meta_vals: DefaultDict = defaultdict(list)
>       meta_keys = [sep.join(val) for val in _meta]
                     ^^^^^^^^^^^^^
E       TypeError: sequence item 0: expected str instance, int found

Details:

  • This error only happens when the record_path argument is provided.
  • No error with ints as keys in record_path
  • Yes error when a single int is passed as meta arg, or when multiple fields are passes (and one of them is an int)

Expected Behavior

pd.json_normalize should use integers as keys in the meta argument.

Installed Versions

>>> import pandas as pd >>> pd.show_versions()

INSTALLED VERSIONS

commit : 4665c10
python : 3.12.7
python-bits : 64
OS : Darwin
OS-release : 24.6.0
Version : Darwin Kernel Version 24.6.0: Mon Jul 14 11:30:40 PDT 2025; root:xnu-11417.140.69~1/RELEASE_ARM64_T8132
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 2.3.2
numpy : 2.3.2
pytz : 2025.2
dateutil : 2.9.0.post0
pip : None
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : 8.4.2
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.2
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

Labels

BugIO JSONread_json, to_json, json_normalize

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions