使用 JsonPath 根据条件从 json 获取值

Get value from json based on condition using JsonPath

我关注了 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"
    }]
}]

}

由此 json 我想获得问题的价值 -> custome_fields -> id 为 167 且预期值为 - 始终(167 可以位于 json 因此不能对其进行硬编码)

请问我怎样才能达到这个价值?

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

结果报错-

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

我得到了和你一样的错误,我不知道为什么。

但是,您可以使用 findAll json 路径方法实现相同的目标,如下所示:

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

打印出来:Always