0

I have JSON file (list.json) as an input:

"Module1": {
  "Description": "",
  "Layer": "1",
  },
  "Vendor": ""
  },
"Module2": {
  "Description": "",
  "Layer": "2",
  },
  "Vendor": ""
  },

"Module3": {
  "Description": "",
  "Layer": "3",
  },
  "Vendor": ""
  },

"Module1": 4
  "Description": "",
  "Layer": "4",
  },
  "Vendor": ""
  },

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 (Module2, Module3).

My code is: import json

list_components = []

with open (/list.json) as f:
    swc_list = json.load(f)

for i in swc_list:
   if i['Vendor'] == 'comp':
    list_components.append(i)

print (list_components)

When I run this code, I keep getting: string indices must be integers on line if i['Vendor'] == 'comp':

What would be the best approach to do this?

2
  • The best approach is to use debugging techniques to align what the variables actually are with what you think they are. Commented May 25, 2021 at 15:18
  • swc_list is not a list. It's a json object. Go through the w3 docs for it. Use pprint for debugging json related things. It will make your life a lot easier Commented May 25, 2021 at 15:28

2 Answers 2

1

You are only missing the fact that your dictionnary of module is a nested one. Each module description is a dictionnary itself.

To iterate through your modules (the keys of your dictionnary) you have to use the .keys() or the .items() method. You can then access every inner dictionnaries.

list_components = []
#iterate through the keys of swc_list
for k in swc_list.keys():
  if swc_list[k]['Vendor'] == 'comp':
    list_components.append(k)

print (list_components)

EDIT:

If you want to take the attributes of your different modules you juste have to access them using their key in the inner dictionary. By specifying which attribute of your dictionary you want you can access its value.

swc_list['Module1'] will give you the entire description dictionnary of the first module

{
        "Description": "",
        "Layer": "1",
        "Vendor": "",
      }

swc_list['Module1']['Layer'] will give you only the value associated with the Layer parameter of th first module

list_components = []

for k in swc_list.keys():
  if swc_list[k]['Vendor'] == 'comp':
    #instead of keeping only the main key (Module) you can append a tuple containing the Module and its attribute Layer 
    list_components.append((k,swc_list[k]['Layer']))

print (list_components)
>>>[('Module2', '1'), ('Module3', '1')]
Sign up to request clarification or add additional context in comments.

2 Comments

One more quick question, this will give us "Module2, Module3" as requested, but what if I want also to take their attrib value like "Layer": "1", so at the end I have somehow Module 2 with "Layer": "1", and Module 2 with "Layer": "1"? I can post another question if u like
Mate I now have a different situation, I will update question, if you find some time to spend please assist, thanks!
0

Rather than filtering a list here you need to filter a dictionary since your modules are stored in a JSON object.

You can use Python dictionary comprehension to do this kind of work:

target_modules = dict((key, val) for key, val in json_data.items() if val['Vendor'] == 'comp')

print(target_modules)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.