Configurable json_normalize with respect to number of levels and Keys to be flattened · Issue #23843 · pandas-dev/pandas (original) (raw)

Code Sample, a copy-pastable example if possible

data = [{"CreatedBy":{"Name": "User001"}, "Lookup":{"TextField":"Some text", "UserField":{"Id":"ID001", "Name": "Name001"}}, "Image":{"a":"b"}}] json_normalize(data).to_json(orient="records")

Current Output

[{"CreatedBy.Name":"User001","Image.a":"b",
"Lookup.TextField":"Some text",
"Lookup.UserField.Id":"ID001","Lookup.UserField.Name":"Name001"}]

Problem description

I want to flatten only specific keys and up to a specific a specific level.
For eg., I want to flatten until level 1 and skip key image.

Expected Output

[{"CreatedBy.Name":"User001",
"Image:{a":"b}",
"Lookup.TextField":"Some text",
"Lookup.UserField":{Id":"ID001",
"Name":"Name001"}]

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
pandas: 0.23.4
pytest: None
pip: 18.1
setuptools: 39.1.0
Cython: None
numpy: 1.15.1
scipy: None
pyarrow: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.7.3
pytz: 2018.4
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 3.0.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None