JSONPath expression to look inside different keys

2019-08-09 00:42发布

问题:

I have a JSON returned by a RESTful API:

{
    "0": {
        "id": "1484763",
        "name": "Name",
        "values": {
            "0": {
                "value": "Peter"
            }
        }
    },
    "1": {
        "id": "2584763",
        "name": "phone",
        "values": {
            "0": {
                "value": "45456456"
            }
        }
    }
}

How do I write a JSONPath that extracts a phone number value? (so in this case, "45456456"). What makes the problem harder, phone number object is not always inside "1" key.

回答1:

Try this JsonPath which results in phone number 45456456

$..[?(@.name = 'phone')].values.0.value

Basically, you have to apply a filter ?() where name == 'phone' and then use normal json path.

Try the expression in this link

JsonPath expression syntax can be found here.



标签: json jsonpath