So I am having here one big JSON file which looks like this:
data = {
"Module1": {
"Description": "",
"Layer": "1",
"SourceDir": "pathModule1",
"Attributes": {
"some",
},
"Vendor": "comp",
"components":{
"Component1": {
"path": "something",
"includes": [
"include1",
"include2",
"include3",
"include4",
"include5"
]
"generated:" "txt"
"memory:" "txt"
etc
},
"Component2":{
"path": "something",
"includes": [
"include1",
"include2",
"include3",
"include4",
"include5"
]
"generated:" "txt"
"memory:" "txt"
etc
}
}
},
"Module2": {
"Description": "",
"Layer": "2",
"SourceDir": "pathModule2",
"Attributes": {
"some",
},
"Vendor": "comp",
"components":{
"Component1": {
"path": "something",
"includes": [
"include1",
"include2",
"include3",
"include4",
"include5"
]
"generated:" "txt"
"memory:" "txt"
etc
},
"Component2":{
"path": "something",
"includes": [
"include1",
"include2",
"include3",
"include4",
"include5"
]
"generated:" "txt"
"memory:" "txt"
etc
}
}
},
"Module3": {
"Description": "",
"Layer": "3",
"SourceDir": "path",
"Attributes": {
"some",
},
"Vendor": "",
},
"Module4": {
"Description": "",
"Layer": "4",
"SourceDir": "path",
"Attributes": {
"some",
}
}
}
I have to go through and take some stuff out of it, so at the end I get this:
Whenever Vendor field is equal to "comp", take that module into consideration, take it's SourceDir filed, all components, their path and includes.
So output would be:
Module1, "pathModule1", components: [Component1, path, [includes: include1, include2 ,include3 ,include4 ,include5 ]], [Component2, path, includes: [include1, include2 ,include3 ,include4 ,include5 ]]
Module2, "pathModule2", components: [Component1, path, [includes: include1, include2 ,include3 ,include4 ,include5 ]], [Component2, path, includes: [include1, include2 ,include3 ,include4 ,include5 ]]
I am really struggling with accessing all the fields that I need.
My current code is this:
with open ("DB.json", 'r') as f:
modules= json.load(f)
for k in modules.keys():
try:
if swc_list[k]["Vendor"] == "comp":
list_components.append(k)
sourceDirList.append(swc_list[k]['SourceDir'])
for i in swc_list[k]['sw_objects']:
list_sw_objects.append((swc_list[k]['sw_objects']))
except KeyError:
continue
I am managing to get only Module1 and sourceDir, but not Component1, 2 and its attributes.. How can I achieve this?
Thanks!