Search code examples
rest-assuredjsonpathrest-assured-jsonpath

Get value from json based on condition using JsonPath


I've following json -

{
"issues": [{
    "id": 200615,
    "custom_fields": [{
        "id": 166,
        "name": "Severity",
        "internal_name": null,
        "field_format": "list",
        "value": "Major"
    }, {
        "id": 167,
        "name": "Reproducibility",
        "internal_name": null,
        "field_format": "list",
        "value": "Always"
    }, {
        "id": 168,
        "name": "Resolution",
        "internal_name": null,
        "field_format": "list",
        "value": "open"
    }]
}]

}

Out of this json I want to get value of issues -> custome_fields -> value where id is 167 and expected value is - Always (167 can be at any position in json hence can't hardcode it)

Could you please how I can achieve the value?

jsonPath.get("issues[0].custom_fields[?(@.id == '167')].value[0]")

results into error -

java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: expecting EOF, found '[' @ line 1, column 50.
   Object.issues[0].custom_fields[?(@.id ==

Solution

  • I get the same error as you do and I don't know why.

    However, you can achieve the same goal using findAll json path method like this:

    issues[0].custom_fields.findAll { it.id == 167 }.value[0]
    

    Prints out: Always