Get outputs from jq on a single line
JsonJqJson Problem Overview
I got below output using: https://stackoverflow.com/a/40330344
(.issues[] | {key, status: .fields.status.name, assignee: .fields.assignee.emailAddress})
Output:
{
"key": "SEA-739",
"status": "Open",
"assignee": null
}
{
"key": "SEA-738",
"status": "Resolved",
"assignee": "[email protected]"
}
But I need to parse each and every line but it's 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?
Expected output:
{ "key": "SEA-739", "status": "Open", "assignee": null }
{ "key": "SEA-738", "status": "Resolved", "assignee": "[email protected]"}
OR
{ "SEA-739", "Open", null }
{ "SEA-738", "Resolved", user2@mycompany.com }
Json Solutions
Solution 1 - Json
-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":"[email protected]"}
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})'
Solution 2 - Json
Not precisely an answer to the long version of the question, but for people who Googled this looking for other single line output formats from jq:
$ jq -r '[.key, .status, .assignee]|@tsv' <<<'
{
"key": "SEA-739",
"status": "Open",
"assignee": null
}
{
"key": "SEA-738",
"status": "Resolved",
"assignee": "[email protected]"
}'
SEA-739 Open
SEA-738 Resolved user2@mycompany.com
@sh
rather than @tsv
returns:
'SEA-739' 'Open' null
'SEA-738' 'Resolved' '[email protected]'
Additionally, there are other output formats to do things such as escape the output, like @html
, or encode it, as with @base64
. The list is available in the Format strings and escaping section of either the jq(1) man page or online at stedolan.github.io/jq/manual.