I'm trying to flatten a JSON file that was originally converted from XML using xmltodict(). There are multiple fields that may have a list of dictionaries. I've tried using record_path with meta data to no avail, but I have not been able to get it to work when there are multiple fields that may have other nested fields. It's expected that some fields will be empty for any given record
I have tried searching for another topic and couldn't find my specific problem with multiple nested fields. Can anyone point me in the right direction?
Thanks for any help that can be provided!
Sample base Python (without the record path)
import pandas as pd import json with open('./example.json', encoding="UTF-8") as json_file: json_dict = json.load(json_file) df = pd.json_normalize(json_dict['WIDGET']) print(df) df.to_csv('./test.csv', index=False) Sample JSON
{ "WIDGET": [ { "ID": "6", "PROBLEM": "Electrical", "SEVERITY_LEVEL": "1", "TITLE": "Battery's Missing", "CATEGORY": "User Error", "LAST_SERVICE": "2020-01-04T17:39:37Z", "NOTICE_DATE": "2022-01-01T08:00:00Z", "FIXABLE": "1", "COMPONENTS": { "WHATNOTS": { "WHATNOT1": "Battery Compartment", "WHATNOT2": "Whirlygig" } }, "DIAGNOSIS": "Customer needs to put batteries in the battery compartment", "STATUS": "0", "CONTACT_TYPE": { "CALL": "1" } }, { "ID": "1004", "PROBLEM": "Electrical", "SEVERITY_LEVEL": "4", "TITLE": "Flames emit from unit", "CATEGORY": "Dangerous", "LAST_SERVICE": "2015-06-04T21:40:12Z", "NOTICE_DATE": "2022-01-01T08:00:00Z", "FIXABLE": "0", "DIAGNOSIS": "A demon seems to have possessed the unit and his expelling flames from it", "CONSEQUENCE": "Could burn things", "SOLUTION": "Call an exorcist", "KNOWN_PROBLEMS": { "PROBLEM": [ { "TYPE": "RECALL", "NAME": "Bad Servo", "DESCRIPTION": "Bad servo's shipped in initial product" }, { "TYPE": "FAILURE", "NAME": "Operating outside normal conditions", "DESCRIPTION": "Device failed when customer threw into wood chipper" } ] }, "STATUS": "1", "REPAIR_BULLETINS": { "BULLETIN": [ { "@id": "4", "#text": "Known target of the occult" }, { "@id": "5", "#text": "Not meant to be thrown into wood chippers" } ] }, "CONTACT_TYPE": { "CALL": "1" } } ] } Sample CSV
| ID | PROBLEM | SEVERITY_LEVEL | TITLE | CATEGORY | LAST_SERVICE | NOTICE_DATE | FIXABLE | DIAGNOSIS | STATUS | COMPONENTS.WHATNOTS.WHATNOT1 | COMPONENTS.WHATNOTS.WHATNOT2 | CONTACT_TYPE.CALL | CONSEQUENCE | SOLUTION | KNOWN_PROBLEMS.PROBLEM | REPAIR_BULLETINS.BULLETIN |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 6 | Electrical | 1 | Battery's Missing | User Error | 2020-01-04T17:39:37Z | 2022-01-01T08:00:00Z | 1 | Customer needs to put batteries in the battery compartment | 0 | Battery Compartment | Whirlygig | 1 | ||||
| 1004 | Electrical | 4 | Flames emit from unit | Dangerous | 2015-06-04T21:40:12Z | 2022-01-01T08:00:00Z | 0 | A demon seems to have possessed the unit and his expelling flames from it | 1 | 1 | Could burn things | Call an exorcist | [{'TYPE': 'RECALL', 'NAME': 'Bad Servo', 'DESCRIPTION': "Bad servo's shipped in initial product"}, {'TYPE': 'FAILURE', 'NAME': 'Operating outside normal conditions', 'DESCRIPTION': 'Device failed when customer threw into wood chipper'}] | [{'@id': '4', '#text': 'Known target of the occult'}, {'@id': '5', '#text': 'Not meant to be thrown into wood chippers'}] |