150

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 }
Bonifacio2
  • 3,405
  • 6
  • 34
  • 54
Jitesh Sojitra
  • 3,655
  • 7
  • 27
  • 46

3 Answers3

246

-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})'
hmedia1
  • 5,552
  • 2
  • 22
  • 27
  • 9
    Long version of -c is --compact-output – eric Aug 25 '21 at 14:38
  • 3
    See this answer https://stackoverflow.com/questions/42178636/how-to-use-jq-to-output-jsonl-one-independent-json-object-per-line . It seems that sometimes you have to run the query twice: `$ curl | jq -c '.[]' | jq -c '.[]' ` – NeilG Oct 17 '21 at 21:44
  • @NeilG That is an array depth issue, not a formatting one. The example you've given is the same as running `jq -c '.[][]'` – hmedia1 Oct 18 '21 at 07:00
  • 3
    Consider also cases when `-j` to remove trailing newline helps. – bishop Apr 05 '22 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? – DragonKnight Nov 04 '22 at 07: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 05 '22 at 09:30
  • 1
    Hmm... I have the same problem with similar input and jq 1.6 always inserts a newline between my fields, whether I have -c or -r. How strange. – Mike S Apr 04 '23 at 17:56
42

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.

bschlueter
  • 3,817
  • 1
  • 30
  • 48
  • 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 01 '20 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: https://stedolan.github.io/jq/manual/#Formatstringsandescaping – vlz Aug 18 '21 at 13:01
0

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:

chi12ldmas01
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
Mike S
  • 1,235
  • 12
  • 19