json-path-expression

Get root element using jsonpath based on sub elements condition

六眼飞鱼酱① 提交于 2020-12-14 11:42:51
问题 I am working with the Jayway JsonPath library to obtain the correct 'id' from below JSON where my phoneNumbers type is 'iPhone'. In general, I would like to know how to find something from the root element of a block when a specific condition is specified in the sub-JSON objects. I tried below expressions that select the block associated with iPhone type and also a list of ids respectively, but I am not able to get to the root element id belonging to the JSON object where my phone type is

Get root element using jsonpath based on sub elements condition

為{幸葍}努か 提交于 2020-12-14 11:41:43
问题 I am working with the Jayway JsonPath library to obtain the correct 'id' from below JSON where my phoneNumbers type is 'iPhone'. In general, I would like to know how to find something from the root element of a block when a specific condition is specified in the sub-JSON objects. I tried below expressions that select the block associated with iPhone type and also a list of ids respectively, but I am not able to get to the root element id belonging to the JSON object where my phone type is

Xpath like query for nested python dictionaries

你。 提交于 2019-11-27 11:40:26
Is there a way to define a XPath type query for nested python dictionaries. Something like this: foo = { 'spam':'eggs', 'morefoo': { 'bar':'soap', 'morebar': {'bacon' : 'foobar'} } } print( foo.select("/morefoo/morebar") ) >> {'bacon' : 'foobar'} I also needed to select nested lists ;) This can be done easily with @jellybean's solution: def xpath_get(mydict, path): elem = mydict try: for x in path.strip("/").split("/"): try: x = int(x) elem = elem[x] except ValueError: elem = elem.get(x) except: pass return elem foo = { 'spam':'eggs', 'morefoo': [{ 'bar':'soap', 'morebar': { 'bacon' : { 'bla':

Xpath like query for nested python dictionaries

∥☆過路亽.° 提交于 2019-11-26 18:03:26
问题 Is there a way to define a XPath type query for nested python dictionaries. Something like this: foo = { 'spam':'eggs', 'morefoo': { 'bar':'soap', 'morebar': {'bacon' : 'foobar'} } } print( foo.select("/morefoo/morebar") ) >> {'bacon' : 'foobar'} I also needed to select nested lists ;) This can be done easily with @jellybean's solution: def xpath_get(mydict, path): elem = mydict try: for x in path.strip("/").split("/"): try: x = int(x) elem = elem[x] except ValueError: elem = elem.get(x)