Home > Blockchain >  Recursive search JSON/DICT in Python 3
Recursive search JSON/DICT in Python 3

Time:02-14

I am realizing in Python 3 some APIs that allow me to receive information about a school based on the class code. But I would like to know how I get the information through the class code. Example: I enter the code GF528S and I want the program to tell me the class (3C INF), the address (Address 1, Milan), and if possible also the name of the school (Test School 1) and the previous keys. Thanks in advance! Of course I use a JSON structure:

{
    "schools": {
         "Lombardia": {
            "Milano": {
                "Milano": {
                    "Test School 1": {
                        "sedi": {
                            "0": {
                                "indirizzo": "Address 1, Milan",
                                "classi": {
                                    "INFORMATICA E TELECOMUNICAZIONI": {
                                        "3C INF": "GF528S"
                                    }                                    
                                }
                            },
                            "1": {
                                "indirizzo": "Address 2, Milan",
                                "classi": {
                                    "INFORMATICA E TELECOMUNICAZIONI": {
                                        "1A IT": "HKPV5P", 
                                        "2A IT": "QL3J3K",
                                        "3A INF": "X4E35C",
                                        "3A TEL": "ZAA7LC"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }   
    }
}

When I get the values ​​from my database they are converted to a python dictionary if it helps!

CodePudding user response:

While I can't write the exact code for you, I think it's reasonable to be able to give you a rough idea of what the code would look like, and some guidance.

I don't exactly know where this JSON data is being obtained. So it may have more / less keys when your applications runs. However, assuming the json is exactly as is, and the json data is loaded onto the variable (let's say json_map), then accessing a specific value looks something like:

json_map[key_value]

So you would want to do something similar to

json_map['schools']['Lombardia']['Milano']

and more keys until you reach the dictionary you want to play around with.

I think the point you might be confused is - if you have multiple values (that you may not be aware of what they might look like) how you handle it. For example, I think the key "sedi" (which I assume means locations) might return multiple locations (i.e. schools) and you won't know what their keys / values are. In that case, you may wish to iterate through that dictionary via something like:

for key, value in dict_.items():
    # do your action

it is likely that key will be an integer (in string format) and value will be another dictionary. You will want to check a specific attribute of the dictionary to see if it's the one you're looking for.

Also, finally, when you get to the 'INFORMATICA E TELECOMUNICAZIONI' dictionary of the location(s), you may wish to return the key of the item that has the corresponding value. Something like:

for key, value in dict_.items():
    if value == 'GF528S':
        return key

Of course, you'll be able to replace this value of 'GF528S' to a variable so you can change it each time.

I think this is as far as I can help you without actually implementing this. I gave the benefit of the doubt that you are like me when I just started programming and I just needed someone to give me a rough outline of what to do. Any more help, I think you may need grab someone who has knowledge of what to do IRL or hire a tutor/teacher to teach you basic concepts of Programming.

CodePudding user response:

search_key = "GF528S"
def recursive_search(dct,keys):
    for key,value in dct.items():
        if key == search_key:
            print(keys,value)
        if type(value) == dict:
            recursive_search(value,[*keys,key])
recursive_search(dinput_dict,[])
  • Related