Twi Twi - 1 year ago 150
JSON Question

Iterating through (JSON?) map in Python

I have a datastructure like that:

sample_map= {'key1': {'internal_key1': ['value1']},
'key2': {'internal_key2': ['value2']},

I would like to iterate throgh on every line key1 and key2. I would like to get the value of 'internal_key1' and 'value_1'variables.

I tried this way:

for keys in sample_map.keys():
for value in sample_map[keys]:
#get internal_keys and values

How should I do this? Someone maybe could tell me about this data structure and the using?

Answer Source

How about the following:

for k,v in sample_map.iteritems():
    print k
    for k1,v1 in v.iteritems():
        print k1,v1[0]

This will print the following:

internal_key2 value2
internal_key1 value1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download