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.
Rocketkey
source share