Reputation: 45
I need to capture all the spaces/chat-rooms they use and ensure the new accounts get connected to all those spaces. I can use curl
to call the service Spark API and get the list of spaces, and process that with jq
to put it in a respectable format. What I end up with is a series of 50 to 900 (depending on the use) JSON objects (one per space/room).
{
"items":[
{
"id":"Y2lzY29zcGFyazovL3VzL1JPT00vNGY0Y2Q5NDAtN2JkZi0xMWU4LThiMjMtMTU0YzAyNTI0YzBh",
"title":"(CISCO INTERNAL): FTB - CMS Opportunity",
"type":"group",
"isLocked":false,
"lastActivity":"2018-06-29T21:05:55.994Z",
"creatorId":"Y2lzY29zcGFyazovL3VzL1BFT1BMRS9kMWI3MDRhNS05ZmU0LTQ3MTYtOGY2Zi02OGEyNjVjZGQ5YzM",
"created":"2018-06-29T20:59:27.316Z"
},
{
"id":"Y2lzY29zcGFyazovL3VzL1JPT00vMTQxODk5MTAtN2JiYS0xMWU4LThiOWUtMjMwOWM4Y2NiYWMz",
"title":"USU Cisco Webex EDU Program",
"type":"group",
"isLocked":true,
"lastActivity":"2018-06-29T19:46:14.993Z",
"creatorId":"Y2lzY29zcGFyazovL3VzL1BFT1BMRS8wZThjMzg3Yy0zNzc5LTRlYTEtYmY2My1mNzZmMDJkYzVjM2M",
"created":"2018-06-29T16:32:56.609Z"
},
{
"id":"Y2lzY29zcGFyazovL3VzL1JPT00vZjYyNDg1NjAtNzg5Yy0xMWU4LTlmYzItNGZhYzQwZWE4MTA4",
"title":"Darren TeamSpace Space",
"type":"group",
"isLocked":false,
"lastActivity":"2018-06-26T15:00:03.332Z",
"teamId":"Y2lzY29zcGFyazovL3VzL1RFQU0vZWQ5Mzc1ZDAtZjE1MC0xMWU1LWFjNWUtNTNiMzQxMDkxMTU1",
"creatorId":"Y2lzY29zcGFyazovL3VzL1BFT1BMRS83YTBkMTJiMS1lODMxLTRkNmItYTAzMS0xMDU4Y2UwOTAwZTU",
"created":"2018-06-25T17:26:57.561Z"
}
]
}
What I'd like to do here is create a quick and dirty script that can then process this JSON file in a loop, pulling the keys in as variables
(items.id = $id, items.title = $title, etc.)
with the associated values from the file, so I can perform some basic "if then else" evaluations (check if isLocked
is true
or false
) and then execute some other API calls using the other variables. For some reason, and it's probably my lack of familiarity with jq
, I can't get the loop and variable extraction to work properly.
Upvotes: 3
Views: 942
Reputation: 85530
You don't need to loop over the JSON objects here, using jq
, you could just interpolate the variable inside each object and do your logic as needed. Since the fields in the JSON could contain spaces between them using a custom delimiter |
to split them and use that in the read
command of the bash
loop
jq --raw-output '.items[] | "\(.id)|\(.title)|\(.isLocked)"' json |
while IFS="|" read -r id title flag; do
printf '%s|%s|%s\n' "$id" "$title" "$flag"
done
Do what ever with the variables id
, title
and flag
in the shell context now. If you want to use more variables, just add them to the original jq
filter.
If you don't want the loop to run in a sub-shell, use the process substitution technique provided by the bash
shell. Doing it runs in the same parent shell and makes the output of commands under <(cmd)
as if it were appear in a temporary file
while IFS="|" read -r id title flag; do
printf '%s|%s|%s\n' "$id" "$title" "$flag"
done< <(jq --raw-output '.items[] | "\(.id)|\(.title)|\(.isLocked)"' json)
Upvotes: 5