data = {'virtualInterfaces': [{"interfaceName": "abc", "interfaceState": "online"}, {"interfaceName": "axc", "interfaceState": "online"}, {"interfaceName": "xpto", "interfaceState": "offline"}]}
foo = "['virtualInterfaces'][2]['interfaceState']"
I need to use the var "foo" to get a specific value from data
e.g
data[foo] #should return "offline"
I tried to use eval
value = eval(data + foo)
But didn´t work.
Foo can have any number of nested list/dict
>Solution :
foo contains the literal indexing string, whereas data is not a string, but the dictionary object. So you have to put data in quotes to use eval:
value = eval('data' + foo)