1

I am trying to pull multiple json keyvalue data within any given JSON object, from a JSON file produced via an API call, for only the JSON objects that contain a specific value within a keyvalue pair; this specific value within a keyvalue pair is obtained via iterating through a list. Hard to articulate, so let me show you:

Here is a list (fake list representing my real list) I have containing the "specific value within a keyvalue pair" I'm interested in:

mylist = ['device1', 'device2', 'device3']

And here is the json file (significantly abridged, obviously) that I am reading from my python script:

[
    {
        "name": "device12345",
        "type": "other",
        "os_name": "Microsoft Windows 10 Enterprise",
        "os_version": null
    },
    {
        "name": "device2",
        "type": "other",
        "os_name": "Linux",
        "os_version": null
    },
    {
        "name": "device67890",
        "type": "virtual",
        "os_name": "Microsoft Windows Server 2012 R2 Standard",
        "os_version": null
    }
]

I want to iterate through mylist, and for each item in mylist that matches the json keyvalue key "name" in the json file, print the values for 'name' and 'os_name'. In this case, I should see a result where after iterating through mylist, device2 matches the 'name' key in the json object that contains {'name':'device2'}, and then prints BOTH the 'name' value ('device2') and the 'os_name' value ('Linux'), and then continues iterating through mylist for the next value to iterate through the json file.

My code that does not work; let us assume the json file was opened correctly in python and is defined as my_json_file with type List:

for i in mylist:
    for key in my_json_file:
        if key == i:
            deviceName = i.get('name')
            deviceOS = i.get('os_name')
            print(deviceName)
            print(deviceOS)

I think the problem here is that I can't figure out how to "identify" json objects that "match" items from mylist, since the json objects are "flat" (i.e. 'device2' in my json file doesn't have 'name' and 'os_name' nested under it, which would allow me to dict.get('device2') and then retrieve nested data).

1 Answer 1

2

Sorry if I did not understand your question clearly, but it appears you're trying to read values off of the list instead of the JSON file since you're looking at i.get instead of key.get when key is what actually contains the information.

And for the optimization issue, I'd recommend converting your list of devices into a set. You can then iterate through the JSON array and check if a given name is in the set instead of doing it the other way. The advantage is that sets can return if they contain an item in O(1) time, meaning it will significantly speed up the overall speed of the program to O(n) where n = size of json array.

for key in my_json_file:
    if key.get('name') in my_list:
        deviceName = key.get('name')
        deviceOS = key.get('os_name')
        print(deviceName)
        print(deviceOS)
Sign up to request clarification or add additional context in comments.

2 Comments

didn't realize I could use the "in" logic for if x in y...this works. I assume the only change I would need to do to make this "optimized" would be to simply the if statement to "if key.get('name') in set(my_list)", yes?
That is correct, but since the condition is in a loop, conversion might take place multiple times. So if you can, it would be a better idea to convert it into a set beforehand.

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.