Jitesh Sojitra Jitesh Sojitra - 1 month ago 13
JSON Question

Get customized output from REST API output using jq linux

Get customized output from REST API output using jq linux

I got below output using:
Extract object/array value from rest API output using unix and jq

But i need to parse each and every lines to its tough to identify which assignee is for which key as far as key group is concerned, is this possible to make one bunch in one row using jq? @peak, @hmedia1 - need your help one more time!

(.issues[] | {key, status: .fields.status.name, assignee: .fields.assignee.emailAddress})


Output:

{
"key": "SEA-739",
"status": "Open",
"assignee": null
}
{
"key": "SEA-738",
"status": "Resolved",
"assignee": "user2@mycompany.com"
}


Postscript



If you want a one-liner that will produce the values as a stream, without quotation marks, consider:

$ jq -r '.issues | ((.[] | (.key, .fields.status.name, .fields.assignee.emailAddress)),length)'


Shows output:

SEA-739
Open
null
SEA-738
Resolved
user2@mycompany.com
2


Expected output:



{ "key": "SEA-739", "status": "Open", "assignee": null }
{ "key": "SEA-738", "status": "Resolved", "assignee": "user2@mycompany.com"}


OR

{ "SEA-739", "Open", null }
{ "SEA-738", "Resolved", user2@mycompany.com }

Answer

-c is what you likely need

Using the output you posted above, you can process it further:

jq -c . input

To Give;

{"key":"SEA-739","status":"Open","assignee":null}
{"key":"SEA-738","status":"Resolved","assignee":"user2@mycompany.com"}

Or you can just change your original command

FROM

jq -r '(.issues[] | {key, status: .fields.status.name, assignee: .fields.assignee.emailAddress})'

TO

jq -c '(.issues[] | {key, status: .fields.status.name, assignee: .fields.assignee.emailAddress})'