Trouble reading froma nested dictionary

Hello,

I am making API calls that return json data like this -

{
    "basicServiceSets": [
        {
            "ssidName": "My SSID",
            "ssidNumber": 0,
            "enabled": true,
            "band": "2.4 GHz",
            "bssid": "8A:15:04:00:00:00",
            "channel": 11,
            "channelWidth": "20 MHz",
            "power": "18 dBm",
            "visible": true,
            "broadcasting": true
        },
        {
            "ssidName": "My SSID",
            "ssidNumber": 0,
            "enabled": true,
            "band": "5 GHz",
            "bssid": "8A:15:14:00:00:00",
            "channel": 64,
            "channelWidth": "40 MHz",
            "power": "18 dBm",
            "visible": true,
            "broadcasting": true
        },
        {
            "ssidName": "My SSID",
            "ssidNumber": 0,
            "enabled": true,
            "band": "6 GHz",
            "bssid": "8A:15:24:00:00:00",
            "channel": 145,
            "channelWidth": "80 MHz",
            "power": "18 dBm",
            "visible": true,
            "broadcasting": true
        }
    ]
}

I want to extract all the “bssid” key/values but I am getting “value doesnt exist” or a blank return.

I realize the issue is that I am searching the “basicservicesets” dictionary for the key rather than the nested dictionary where the key actually is.

I think the syntax for looking into the nested ones is something like this
print(myfamily["child2"]["name"])

however the nested dictionaries in the api data dont appear to have names so I cant specify them :frowning:

How do I get into the nested dictionaries if they have no names?

Thanks!

Here is one way

import json 
file = 'Python/data.json'

with open(file, 'r') as json_file:
    data = json.load(json_file)

for details in data.values():
    for data in details:
            print(data['bssid'])

Hi, this is just a random example of the correct syntax I got from a learning site to show how I thought it should work (by specifying the dictionary name. I couldnt use an example from my own scenario as my data doesnt have the names).

thank you!
I dont really know much about iterators - I have just read this to try and understand and I get the principles but still dont fully get the implementation of it.

In your example, it looks like you load the response data into a variable called “file”

and then this bit opens the file, reads the data and then loads it into the “data” variable?

with open(file, 'r') as json_file:
    data = json.load(json_file)

and then this bit iterates through “data” using the “values method”

this is where I get most fuzzy - so “.values” goes through “data” and creates a list of values and then loads that list into a new variable called “details”?

for details in data.values():
  

and then in the second loop it goes through the list in “details” and extracts+prints the value of bssid?

  for data in details:
            print(data['bssid'])

I am not sure why “details” needs to exist. It looks like we load values from “data” into it and then look in it for those values. Why not just look into “data” directly?

Thanks!

Hi Randell,

I fully understand that school of thought is best for a lot of people, however I have found when learning coding, I actually learn a lot better by seeing the solution first and then working through it step by step to make sure I understand exactly what every step is doing and how/why it works.

I’ve commented the code. Hope it helps.

import json 
file = 'Python/data.json'

# Open and load data.json. data.json is a list of dicts
with open(file, 'r') as json_file:
    data = json.load(json_file)

# Using details to loop through the list
# and access the dicts
for details in data.values():
    # Reassigning data variable to acces the dict keys
    # data could be named stats or any other python variable naming convention
    # print(data['bssid]) is accessing the data key for the value
    for data in details:
            print(data['bssid'])

oh is it because of the nested dictionaries? “data” is a list of dictionaries so its loading the values of each subdictionary into “details” so we have a list of the actual key values in all the lists?

thank you! I have code that is now working in that it returns all the bssids.

        for details in wireless_status.values():
            for wireless_status in details:
                print(wireless_status['bssid'])

Now I am trying to refine it so it only returns bssids where the “ssidName” doesnt contain the text “unconfigured”

so, trying to load bssid values into “d” where the “ssidName” value does not contain the text “unconfigured”

wireless_status = json.loads(wireless_response.content)

        for details in wireless_status.values():
            for wireless_status in details:
                bssids = [d["bssid"] for d in wireless_status if "Unconfigured" not in d["ssidName"]]
                print(bssids)

am I over-complicating this? it returns an error -
TypeError: string indices must be integers, not ‘str’

But a dictionary type is “dict” isnt it? So I’m guessing that d[“bssid”] or d[“ssidName”] is being interpreted as a string? and its is saying I cant search a string with a string (Unconfigured) ?

never mind! found a solution…

think I was over-complicating it. This si what I have ended up with which seems to work

        for basicServiceSet in wireless_status['basicServiceSets']:
            if "unconfigured" not in basicServiceSet['ssidName'].lower():
                print(basicServiceSet['bssid'])

(basicserviceset) is the name of the nested dictionarys in the return data

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.