Home > database >  Python parse nested JSON file and take out needed attributes
Python parse nested JSON file and take out needed attributes

Time:11-04

so here is my JSON file:

data = {
        "Module1": {
            "Description": "",
            "Layer": "1",
            "SourceDir": "pathModule1",
            "Attributes": {
                "some",
            },
            "Vendor": "comp",
            "components":{
                "Component1": {
                   "path": "pathToCom1",
                   "includes": [
                       "include1",
                       "include2",
                       "include3",
                       "include4",
                       "include5"
                   ]
                   "sw_items:"{
                      "sw_item1:"{
                       ...(something)
                       }
                      "sw_item2:"{
                       ...(something)
                       }
                      "sw_item3:"{
                       ...(something)
                       }
                    }
                   "generated:" "txt"
                   "memory:" "txt"
                   etc
                },
                "Component2":{
                   "path": "pathToCom2",
                   "includes": [
                       "include1",
                       "include2",
                       "include3",
                       "include4",
                       "include5"
                   ]
                   "sw_items:"{
                      "sw_item1_2:"{
                       ...(something)
                       }
                      "sw_item2_2:"{
                       ...(something)
                       }
                      "sw_item3_3:"{
                       ...(something)
                       }
                   "generated:" "txt"
                   "memory:" "txt"
                   etc
                }
            }
        },
        "Module2": {
            "Description": "",
            "Layer": "2",
            "SourceDir": "pathModule2",
            "Attributes": {
                "some",
            },
            "Vendor": "comp",
            "components":{
                "Component1": {
                   "path": "pathToCom1",
                   "includes": [
                       "include1",
                       "include2",
                       "include3",
                       "include4",
                       "include5"
                   ]
                   "sw_items:"{
                      "sw_item1:"{
                       ...(something)
                       }
                      "sw_item2:"{
                       ...(something)
                       }
                      "sw_item3:"{
                       ...(something)
                       }
                   "generated:" "txt"
                   "memory:" "txt"
                   etc
                },
                "Component2":{
                   "path": "pathToCom2",
                   "includes": [
                       "include1",
                       "include2",
                       "include3",
                       "include4",
                       "include5"
                   ]
                   "sw_items:"{
                      "sw_item1_2:"{
                       ...(something)
                       }
                      "sw_item2_2:"{
                       ...(something)
                       }
                      "sw_item3_3:"{
                       ...(something)
                       }
                   "generated:" "txt"
                   "memory:" "txt"
                   etc
                }
            }
        },
        "Module3": {
            "Description": "",
            "Layer": "3",
            "SourceDir": "path",
            "Attributes": {
                "some",
            },
            "Vendor": "",
        },
        "Module4": {
            "Description": "",
            "Layer": "4",
            "SourceDir": "path",
            "Attributes": {
                "some",
            }
        }
    }

I need to write into list (list_sw_items) only names of all sw_items.

So at the end list_sw_items = [sw_item1, sw_item2, sw_item3, sw_item1_2, sw_item2_2, sw_item3_2, sw_item1, sw_item2 , sw_item3 ]

my code for now, where I keep getting error (KeyError) on sw_items

for k,v in data.items():
    for i,n in v['sw_items'].items():

What would be a way do achieve this?

Thanks!

CodePudding user response:

for k,v in data.items():
   for i,n in v['components'].items():
        for j, l in n['sw_items'].items():
  • Related