groovy - Gpath when an expression is true -
from json below, trying write gpath return "state" field when "type" field equal ride_state (i.e. want 2 "open" states)
[{ "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" } }]
i new groovy , gpath , far haven't been able find way this, please help?
with valid json, can done below:
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']
Comments
Post a Comment