Iterating nested json in python -


i have json in format. how can print each value , go inside objects. also, json can vary , name, need generic solution.

output = {'name':'stackoverflow',        'competitors':[{   'competitor':'bing',                           'link':'bing.com'},                       {   'competitor':'google',                           'link':'google.com'}],        'acquisition': {'acquired_day': 16,                        'acquired_month': 12,                        'acquired_year': 2013,                        'acquiring_company': {'name': 'viggle',                                              'permalink': 'viggle'}}}  

you can use isinstance check if dict or list. may work, haven't checked it.

output = {'name': 'stackoverflow',       'competitors': [{'competitor': 'bing',                        'link': 'bing.com'},                       {'competitor': 'google',                        'link': 'google.com'}],       'acquisition': {'acquired_day': 16,                       'acquired_month': 12,                       'acquired_year': 2013,                       'acquiring_company': {'name': 'viggle',                                             'permalink': 'viggle'}}}   def traverse(obj):     if isinstance(obj, dict):         key, value in obj.iteritems():             print('dict_key', key)             traverse(value)     elif isinstance(obj, list):         value in obj:             traverse(value)     else:         print('value', obj)   traverse(output) 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -