Python Iterar over JSON nested with variable values

0

Hi, my friends, please excuse me, you could help me iterate on this json that returns the request I made with requests but I already tried with items () and values () but nothing else sends me errors or at least tell me what I'm doing wrong was for example tried to access User-Agent

Try now with

   for i in json:
       for j in i:

and nothing

Also try the way

    for key,value in json.items()
         for key2,value2 in value.items()

Here I leave the dictionary

            json = {
                    "args": {
                    "curso": "python", 
                    "nombre": "eduardo"
                    }, 
                    "headers": {
                    "Accept": "*/*", 
                    "Accept-Encoding": "gzip, deflate", 
                    "Connection": "close", 
                    "Host": "httpbin.org", 
                    "User-Agent": "python-requests/2.21.0"
                    }, 
                   "origin": "201.141.214.165", 
                   "url": "http://prueba.html"
                    }

Thank you very much and greetings

    
asked by kisk 29.12.2018 в 06:17
source

1 answer

0

It should work, here's a code that works:

json = {
    "args": {
        "curso": "python",
        "nombre": "eduardo"
    },
    "headers": {
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate",
        "Connection": "close",
        "Host": "httpbin.org",
        "User-Agent": "python-requests/2.21.0"
    },
    "origin": "201.141.214.165",
    "url": "http://prueba.html"
}
for (key, val) in json.items():
    if type(val) is dict:
        for (key1, val2) in val.items():
            print("{}.{}: {}".format(key, key1, val2))
    else:
        print("{}: {}".format(key, val))

Specifically it works for your data set, but I guess it will not always be the same, so here is another form that works independently of the structure of the dictionary.

json = {
    "args": {
        "curso": "python",
        "nombre": "eduardo"
    },
    "headers": {
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate",
        "Connection": "close",
        "Host": "httpbin.org",
        "User-Agent": "python-requests/2.21.0"
    },
    "anidado": {
        "anidado1": {
            "a11": {
                "a111": "hola"
            }
        },
        "anidado2": {
            "a21": {
                "a211": {
                    "a2111": "hola otra vez"
                }
            },
            "a22": "hola bye"
        }
    },
    "origin": "201.141.214.165",
    "url": "http://prueba.html"
}

def print_properties(value, parent):
    if type(value) is dict:
        for (key, val) in value.items():
            if type(val) is dict:
                print_properties(val, parent + '.' + key)
            else:
                print("{}: {}".format(parent + '.' + key, val))
    else:
        print("{}: {}".format(parent, value))


for (key, val) in json.items():
    print_properties(val, key)

By the way, in Python there are no JSON's, they are called dictionaries, but an associative array is the same.

I hope it serves you.

    
answered by 29.12.2018 в 08:08