Find a value within nested json dictionary in python

前端 未结 3 1197
萌比男神i
萌比男神i 2020-12-28 09:04

From the following json, in python, I\'d like to extract the value \"TEXT\". All the keys are constant except for unknown. Unknown could be any string like \"a6784t66\" or \

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-28 09:42

    You can use a recursive function to dig through every layer and print its value with an indent

    def recurse_keys(df, indent = '  '):
        ''' 
        import json, requests, pandas
        r = requests.post(...)  
        rj = r.json() # json decode results query
        j = json.dumps(rj, sort_keys=True,indent=2)            
        df1 = pandas.read_json(j)         
        '''
        for key in df.keys():
            print(indent+str(key))
            if isinstance(df[key], dict):
                recurse_keys(df[key], indent+'   ')
    recurse_keys(df1)
    

提交回复
热议问题