0

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

3
  • This is not a valid json Commented Aug 19, 2021 at 10:27
  • Updated just now Commented Aug 19, 2021 at 10:29
  • 1
    As a general principle: Keep in mind JSON is just a format for serializing some data structure, in the case of Python typically a 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 a KeyError so it would be better to post the exact error message and the full traceback. Commented Aug 19, 2021 at 11:30

1 Answer 1

1

When iterating through dictionaries, it's often easier to use .items() that returns an iterator over key and value pairs, as in

for key, value in {}.items():
    pass

The error you get error: keyError x implies that x is not in the dictionary. If you are uncertain if the key is present or not, you could use the get(key, default) command instead, as

x = {}.get('x', 'default')
assert x == 'default'

Here's your code after I edited it by using .items() to iterate though the dictionary. Since the input data was unclear, I took some freedom in editing it.

data = {
    "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": "",
    },
    "Module4": {
        "Description": "",
        "Layer": "4",
        "SourceDir": "path",
        "Attributes": {
            "some",
        },
        "Vendor": "comp",
    },
}

and the actual code

list_components = []
sourceDirList = []
combined_list = []

for (key, value) in data.items():
    if value["Vendor"] == "comp":
        source_dir = value['SourceDir']
        list_components.append(key)
        sourceDirList.append(source_dir)

        combined_list.append([key, source_dir])

The output is

>>> print(list_components)
['Module1', 'Module4']

>>> print(sourceDirList)
['path', 'path']

>>> print(combined_list)
[['Module1', 'path'], ['Module4', 'path']]

>>> print(list(zip(list_components, sourceDirList)))
[('Module1', 'path'), ('Module4', 'path')]

Edit: Combine module name and resulting path into lists of tuples.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the assistance man! Thing is that I need that in format: [Module 1, path], [Module4, path]
I added the answer so that it also outputs lists with tuples.
Thanks mate this works! What would be the best way to make an exception when some of the modules do not actually have "Vendor" field? In this case script will just break when run to such case
I found it, there is an exception catch with: except KeyError
An alternative to use try-catch could be to use the dictionary's get(key, fallback) that would return the fallback variable if the key is missing. What works best varies between situations.

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.