表达式为真时的 Gpath

Gpath when an expression is true

从下面的 JSON 开始,我正在尝试编写一个 Gpath,当 "type" 字段等于 [=15 时,它只会 return "state" 字段=]ride_state(即我只想要两个 "open" 状态)

[{
    "type": "day",
    "data": {
        "id": "7a46a975-5474-4278-a56f-4deadd9a276d",
        "state": "closed"
    }
}, {
    "type": "ride_state",
    "data": {
        "id": "13f3b625-b39e-4875-b29c-0173712b3c87",
        "state": "open"
    }
}, {
"type": "ride_state",
    "data": {
        "id": "b52b5081-e1df-42da-ba76-027c63205f8e",
        "state": "open"
     }
}]

我是 Groovy 和 Gpath 的新手,到目前为止还没有找到解决这个问题的方法,有人可以帮忙吗?

有了有效的 json,可以按如下方式完成:

def jsonString = """
[{
    "type": "day",
    "data": {
        "id": "7a46a975-5474-4278-a56f-4deadd9a276d",
        "state": "closed"
    }
}, {
    "type": "ride_state",
    "data": {
        "id": "13f3b625-b39e-4875-b29c-0173712b3c87",
        "state": "open"
    }
}, {
    "type": "ride_state",
    "data": {
        "id": "b52b5081-e1df-42da-ba76-027c63205f8e",
        "state": "open"
    }
}]
"""

assert new groovy.json.JsonSlurper()
                      .parseText( jsonString )
                      .findResults {it.type == 'ride_state' ? it.data.state : null} == 
                      ['open', 'open']