1

I don't know if that's the proper title for my question. Please pardon me with that.

The code that I've tried is not getting the exact output that I want. This is my code that I've tried:

   curl -s --request GET \
    http://10.10.5.242/api/v1/incidents \
    -H "Content-Type: application/json;" \
    -H "X-Cachet-Token: ROvbssneyYwR8fwNgOWj" \
     | json_pp | grep -e id -e component_id

And it will output this

 "component_id" : "4",
 "id" : 1,
 "id" : 2,
 "component_id" : "4",
 "id" : 3,
 "component_id" : "4",
 "component_id" : "4",
 "id" : 4
 "component_id" : "3",
 "id" : 5,
 "component_id" : "2",
 "id" : 6,

This is the content of the api the I'm using http://10.10.5.242/api/v1/incidents

{
        "meta": {
            "pagination": {
                "total": 6,
                "count": 6,
                "per_page": 20,
                "current_page": 1,
                "total_pages": 1,
                "links": {
                    "next_page": null,
                    "previous_page": null
                }
            }
        },
        "data": [
            {
                "id": 1,
                "component_id": "4",
                "name": "Service Unavailable",
                "status": "4",
                "visible": 1,
                "message": "Server is not responding",
                "scheduled_at": "2018-02-26 10:05:03",
                "created_at": "2018-02-23 14:56:16",
                "updated_at": "2018-02-26 08:37:11",
                "deleted_at": null,
                "human_status": "Fixed"
            },
            {
                "id": 2,
                "component_id": "4",
                "name": "Service Unavailable",
                "status": "4",
                "visible": 1,
                "message": "Server is not responding",
                "scheduled_at": "2018-02-26 10:05:03",
                "created_at": "2018-02-23 15:39:52",
                "updated_at": "2018-02-26 08:37:11",
                "deleted_at": null,
                "human_status": "Fixed"
            },
            {
                "id": 3,
                "component_id": "4",
                "name": "Service Unavailable",
                "status": "4",
                "visible": 1,
                "message": "Server is not responding",
                "scheduled_at": "2018-02-26 10:05:03",
                "created_at": "2018-02-26 08:15:43",
                "updated_at": "2018-02-26 08:37:12",
                "deleted_at": null,
                "human_status": "Fixed"
            },
            {
                "id": 4,
                "component_id": "4",
                "name": "Service Unavailable",
                "status": "4",
                "visible": 1,
                "message": "Server is not responding",
                "scheduled_at": "2018-02-26 10:05:03",
                "created_at": "2018-02-26 08:19:12",
                "updated_at": "2018-02-26 08:37:12",
                "deleted_at": null,
                "human_status": "Fixed"
            },
            {
                "id": 5,
                "component_id": "3",
                "name": "Service Unavailable",
                "status": "2",
                "visible": 1,
                "message": "Server is not responding",
                "scheduled_at": "2018-02-26 10:05:03",
                "created_at": "2018-02-26 10:01:32",
                "updated_at": "2018-02-26 10:01:32",
                "deleted_at": null,
                "human_status": "Identified"
            },
            {
                "id": 6,
                "component_id": "2",
                "name": "Service Unavailable",
                "status": "2",
                "visible": 1,
                "message": "Server is not responding",
                "scheduled_at": "2018-02-26 10:05:03",
                "created_at": "2018-02-26 10:03:38",
                "updated_at": "2018-02-26 10:03:38",
                "deleted_at": null,
                "human_status": "Identified"
            }
        ]
    }

The output that I want is to get all the id of "component_id": "4", and will output this

"id" : 1,
"id" : 2,
"id" : 3,
"id" : 4,

What I've wanted is grep the component_id and will get the all of the values of id of component id. Cause my plan is to bring those values in my for loop.

2 Answers 2

3
$ # with jq
$ curl .. | jq . | grep -e id -e component_id
      "id": 1,
      "component_id": "4",
      "id": 2,
      "component_id": "4",
      "id": 3,
      "component_id": "4",
      "id": 4,
      "component_id": "4",
      "id": 5,
      "component_id": "3",
      "id": 6,
      "component_id": "2",

$ curl .. | jq . | awk '/"component_id": "4"/{print pl} {pl=$0}' 
      "id": 1,
      "id": 2,
      "id": 3,
      "id": 4,

Also, probably jq alone might help, couldn't get in exact required output format

$ curl .. | jq '.data[] | select(.component_id == "4").id'
1
2
3
4


$ # use this for exact data shown in OP
$ curl .. | json_pp | grep -e id -e component_id | awk '/"component_id"/{f=match($0,/"4",/); next} f'
 "id" : 1,
 "id" : 2,
 "id" : 3,
 "id" : 4
Sign up to request clarification or add additional context in comments.

Comments

2

As always, you should prefer a tool like jq over regex for parsing out values from JSON.

bash$ jq -r '.data[] | select(.component_id == "4") | "\"id\": \(.id)," ' file
"id": 1,
"id": 2,
"id": 3,
"id": 4,

The resulting output is not valid JSON so I think the -r is inescapable. The string substitution syntax I got from https://github.com/stedolan/jq/issues/48#issuecomment-11561165

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.