I have a json file as an input:
"Module1": {
"Description": "",
"Layer": "1",
"SourceDir": "path"
"Attributes": {
"some",
},
"Vendor": "comp"
},
"Module2": {
"Description": "",
"Layer": "2",
"SourceDir": "path"
"Attributes": {
"some",
},
"Vendor": ""
},
"Module3": {
"Description": "",
"Layer": "3",
"SourceDir": "path"
"Attributes": {
"some",
},
"Vendor": ""
},
"Module1": 4
"Description": "",
"Layer": "4",
"SourceDir": "path"
"Attributes": {
"some",
},
"Vendor": "comp"
},
I am trying to extract all modules (their names) if "Vendor" matches to some criteria, in this case all of the modules with "Vendor": "comp" should be printed together with its source dir (Module1, Module4).
My code is:
import json
for k in swc_list.keys():
if swc_list[k]['Vendor'] == 'comp':
list_components.append(k)
sourceDirList.append(swc_list[k]['SourceDir'])
I keep getting error: keyError x
dict. So if you have no problem actually reading the JSON file, then the question about how to manipulate that data structure doesn't really have anything to do with JSON, and you might have better luck finding an existing answer to your problem once taking JSON out of the equation. From your example it's not clear at all why you would get aKeyErrorso it would be better to post the exact error message and the full traceback.