Skip to content

json_normalize errors if meta fields are integer #32480

Closed
@joetl

Description

@joetl

Code Sample, a copy-pastable example if possible

import pandas as pd

data = [{"person_id": "test", 
         "test_field": 1,  
         "access": [{"step": 1, 
                     "phase":"starting"}, 
                    {"step":2,
                     "phase":"ending"}
                    ]
         }]

df = pd.json_normalize(data,"access",meta=["person_id","test_field"])

print(df)

Problem description

If meta fields are integer we get the below error. json_normalize works fine for string datatype
Traceback (most recent call last):
File "C:/Users/HOME/PycharmProjects/pandas_test/pandas_test.py", line 12, in
df = pd.json_normalize(data,"access",meta=["person_id","test_field"])
File "C:\Users\HOME\Anaconda3\envs\pandas_test\lib\site-packages\pandas\io\json_normalize.py", line 327, in _json_normalize
_recursive_extract(data, record_path, {}, level=0)
File "C:\Users\HOME\Anaconda3\envs\pandas_test\lib\site-packages\pandas\io\json_normalize.py", line 314, in _recursive_extract
meta_val = _pull_field(obj, val[level:])
File "C:\Users\HOME\Anaconda3\envs\pandas_test\lib\site-packages\pandas\io\json_normalize.py", line 246, in _pull_field
f"{js} has non iterable value {result} for path {spec}. "
TypeError: {'person_id': 'test', 'test_field': 1, 'access': [{'step': 1, 'phase': 'starting'}, {'step': 2, 'phase': 'ending'}]} has non iterable value 1 for path ['test_field']. Must be iterable or null.

Expected Output

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.6.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : AMD64 Family 16 Model 10 Stepping 0, AuthenticAMD
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None

pandas : 1.0.1
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 45.2.0.post20200210
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions