139

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": "user2@mycompany.com"
 }

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": "user2@mycompany.com"}

OR

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

2 Answers 2

230

-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})'

6
  • 6
    Long version of -c is --compact-output
    – eric
    Aug 25, 2021 at 14:38
  • 3
    See this answer stackoverflow.com/questions/42178636/… . It seems that sometimes you have to run the query twice: $ curl | jq -c '.[]' | jq -c '.[]'
    – NeilG
    Oct 17, 2021 at 21:44
  • 3
    Consider also cases when -j to remove trailing newline helps.
    – bishop
    Apr 5, 2022 at 13:59
  • 1
    -c creates problem if value has space in it. like {"randKey":"rand value"}. in this case jq only reads till {"randKey":"rand and the rest will be ignored. whats the solution? Nov 4, 2022 at 7:36
  • 2
    @DragonKnight That’s an external issue, nothing to do with jq. You must be delimiting spaces in your output or similar
    – hmedia1
    Nov 5, 2022 at 9:30
41

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": "user2@mycompany.com"
 }'

outputs:

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

@sh rather than @tsv returns:

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

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.

2
  • The @tsv is crucial, without that (or the @sh) my output had each value on its own line. Helpful answer! I wasn't able to find this "string" version (instead of json format) anywhere else.
    – alec
    Jul 1, 2020 at 18:57
  • 3
    Very helpful! Note that the answer uses -r for proper output. Other options are @csv, @text, @base64 and options for escaping like @uri, @html. The relevant manual section is here: stedolan.github.io/jq/manual/#Formatstringsandescaping
    – vlz
    Aug 18, 2021 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.