jq with regex in curl output

I have a below JSON

{
    "router": {
        "node_id": "ip-1-2-3-4",
        "state": "HEALTHY",
        "message": "OK"
    },
    "services": [
        {
            "service_id": "test@1234",
            "state": "HEALTHY",
            "message": "OK"
        },
        {
            "service_id": "something@45678",
            "state": "NOT HEALTHY",
            "message": "OK"
        }

I want to get the state, i can get that for first part of json but not for the rest

working:

curl -sk https://example/test | jq -r .router.state
  1. How can i get the state for rest of the JSON under services only if it matches "service_id": "test@*"

curl -sk https://example/test | jq -r .services.state doesn’t seems to work

  1. How can i add regex to above command to match service_id value

>Solution :

.services[] | select(.service_id | test("test@*")).state

Will output .state where service_id matches the test@* regex.

If no matches are found, nothing is returned.

Online demo

Leave a Reply