173

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", [email protected] }

4 Answers 4

281

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

7
  • 15
    Long version of -c is --compact-output
    – eric
    Commented 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
    Commented Oct 17, 2021 at 21:44
  • 3
    Consider also cases when -j to remove trailing newline helps.
    – bishop
    Commented 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? Commented 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
    Commented Nov 5, 2022 at 9:30
46

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]"
 }'

outputs:

SEA-739 Open
SEA-738 Resolved        [email protected]

@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.

2
  • 1
    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
    Commented 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
    Commented Aug 18, 2021 at 13:01
5

The answers with "-c" and "-r" didn't work for me with jq version 1.6. What I did was the following:

Given input like

{
  "parameters": {
    "puppetmaster": "",
    "ansible_job_template_id": "117",
    "lifecycle_environment": "Dev",
    "hostname": "childmas01",
}

And a jq command like

jq -r '.parameters | .hostname,.lifecycle_environment' filename

I got output like this:

childmas01
Dev

I got the same results with -c instead of -r, and also with -cr. So I simply made the results into an array, and joined them with a space, like this:

jq -r '.parameters | [ .hostname,.lifecycle_environment ] | join(" ")'  filename

To get this:

childmas01 Dev
2
  • I love you Mike!
    – sloppy
    Commented Jun 11 at 15:06
  • 1
    @sloppy LoL I love you too, man. In a sloppy kind of way.
    – Mike S
    Commented Jun 20 at 13:19
0

So when working on my script and researching I found this question. After some tinkering this is what I found worked for me. Included various styles of outputs I got so people can pick what works for them.

$ cat config/menus.json | jq -c '.menus[] | select(.name == "main") | (.columns[])' 
"value1"
"value2"
"value3"
"value4"
"value5"
$ cat config/menus.json | jq -c '.menus[] | select(.name == "main") | [.columns[]]' 
["value1","value2","value3","value4","value5"]
$ cat config/menus.json | jq -c '.menus[] | select(.name == "main") | [.columns[]] | @sh' 
"'value1' 'value2' 'value3' 'value4' 'value5'"
$ cat config/menus.json | jq -r '.menus[] | select(.name == "main") | [.columns[]] | @sh' 
'value1' 'value2' 'value3' 'value4' 'value5'
New contributor
itzteajay is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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