Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading