question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Configurable json_normalize with respect to number of levels and Keys to be flattened

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

3reactions
bhavaniravicommented, Nov 23, 2018

@WillAyd I am a bit confused with the behavior ofjson_normalize method with records_path. While just passing a list of records flattens the dictionary, it doesn’t flattens the same on sending records_path param. Is it the excepted behavior?

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

json_normalize(data, record_path=["Lookup"])

Current Output

   TextField                           UserField
0  Some text  {'Id': 'ID001', 'Name': 'Name001'}
1  Some text  {'Id': 'ID001', 'Name': 'Name001'}

Expected Output

   TextField              UserField.Id       Name
0  Some text               ID001             Name001
1  Some text               ID001             Name001
0reactions
bhavaniravicommented, Jun 28, 2019

okay, will create another one.

Read more comments on GitHub >

github_iconTop Results From Across the Web

All Pandas json_normalize() you should know for flattening ...
We can see that no error is thrown and those missing keys are shown as NaN . 2. Flattening a JSON with multiple...
Read more >
pandas.json_normalize — pandas 1.5.2 documentation
Normalize semi-structured JSON data into a flat table. Parameters ... Max number of levels(depth of dict) to normalize. if None, normalizes all levels....
Read more >
How to fix json_normalize when it cannot iterate over column ...
I want to take the phone_numbers column and flatten the information inside of it so I can query say the 'id' field. When...
Read more >
Flatten transformation in mapping data flow - Microsoft Learn
Denormalize hierarchical data using the flatten transformation in Azure Data Factory and Synapse Analytics pipelines.
Read more >
Nested dictionary json to dataframe - sono naturale
Sample JSON file; Convert to DataFrame; Extract and flatten; ... The “major” and “minor” key in each of these objects is in a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found