Find value in json nested dictionary in python

From the following json, in python, I would like to extract the value "TEXT". All keys are permanent except unknown. Any string like "a6784t66" or "hobvp * nfe" can be unknown. The value of the unknown is unknown , only that it will be in that position in every json response.

{ "A": { "B": { "unknown": { "1": "F", "maindata": [ { "Info": "TEXT" } ] } } } } 

single line json

 '{"A":{"B":{"unknown":{"1":"F","maindata":[{"Info":"TEXT"}]}}}}' 

How would you get the value "Text"? (I know how to load json from json.loads) .. but I'm not sure how to get the value "Text". Thanks.

(I'm not sure what the best title is.)

+7
source share
3 answers

This is a little longer, but in this example above:

 In [1]: import json In [2]: s = """\ ...: { ...: "A": { ...: "B": { ...: "unknown": { ...: "1": "F", ...: "maindata": [ ...: { ...: "Info": "TEXT" ...: } ...: ] ...: } ...: } ...: } ...: }""" In [3]: data = json.loads(s) In [4]: data['A']['B']['unknown']['maindata'][0]['Info'] Out[4]: u'TEXT' 

Basically, you treat it like a dictionary, passing in the keys to get the values ​​of each nested dictionary. The only other part is when you click maindata where the resulting value is a list. To deal with this, we pull out the first element [0] , and then turn to the Info key to get the TEXT value.

If unknown changes, you replace it with a variable that represents the "known" name that it will take at this point in your code:

 my_variable = 'some_name' data['A']['B'][my_variable]['maindata'][0]['Info'] 

And if I really read your question correctly the first time, if you don’t know what unknown at any time, you can do something like this:

 data['A']['B'].values()[0]['maindata'][0]['Info'] 

Where values() is a variable containing:

 [{u'1': u'F', u'maindata': [{u'Info': u'TEXT'}]}] 

A list of individual items that can be accessed using [0] , and then you can proceed as described above. Please note that this depends on the fact that there is only one element in this dictionary - you need to adjust a little if there were more.

+15
source

As you said, the unknown was in a fixed place. You can do the following

 import json s=json.loads('{"A":{"B":{"unknown":{"1":"F","maindata":[{"Info":"TEXT"}]}}}}') i=s["A"]["B"].keys() x=i[0] # Will store 'unknown' in x, whatever unknown is print s['A']['B'][x]['maindata'][0]['Info'] #here x dictionary index is used after B as its value will be the value for unknown 

This should complete the task, since only the unknown key is truly "unknown"

+2
source

You can use a recursive function to scroll through each layer and print its value using indentation

 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) 
+2
source

All Articles