Go to All Forums

parsing data from the API using jq


I want to create 80 canned reports, one for each monitoring group.  I don't want to do this by hand, that's what shell and the API is for.


This works ok:

  1. # curl https://www.site24x7.com/api/monitor_groups     -H "Accept: application/json; version=2.0"     -H "Authorization: Zoho-authtoken $token" | jq  '.[] '

But after that ..

  1. # curl https://www.site24x7.com/api/monitor_groups     -H "Accept: application/json; version=2.0"     -H "Authorization: Zoho-authtoken $token" | jq  '.group_id'
  2. null
I'm reading the jq manual and tutorials but .. I could use a few tips, if you've got any.

Edit;

The point of the exercise is to pull in the monitor group ID, and display name, then use those values to create the report.
Like (1) Reply
Replies (1)

Re: parsing data from the API using jq

Hi Brian,

Response object of all our APIs will have the requested detail under “data” attribute. “data” attribute will hold either an array or an object based on the API.

{
code: “0”,
message: “success”,
data: [  ],
}

jq filter .[] is actually returning values of these three attributes only. But the group details are present under data attribute. In this monitor_groups API “data” attribute will hold an array of group objects. To fetch the first monitor group's id, you have to use the following jq filter

'.data[0].group_id'

To iterate to through all group ids, use the following filter

'.data[].group_id'

To create a JSON array of group ids,

'[.data[].group_id]'

To create a JSON array of objects with group ids and names,

'[.data[] | {id: .group_id, name: .display_name}]'

Hope this helps. Share your final report format here. We will consider having it as a built-in report, if it could be useful to our other users as well.

Thillai.
Like (0) Reply

Was this post helpful?