-1

[NOTE: The question has been clarified in accordance with the comments, some of which might therefore be outdated.]

I'm using jq to parse the following JSON:

[{
    "type": "A",
    "id": "A",
    "name": "A",
    "message": "OK",
    "updateDate": "2018-07-31T10:55:14.813Z",
    "createDate": "2018-07-31T08:01:32.164Z",
    "status": "ACTIVE"
}, {
    "type": "B",
    "id": "B",
    "name": "B",
    "message": "OK",
    "updateDate": "2018-07-31T10:55:16.803Z",
    "createDate": "2018-07-31T08:01:34.171Z",
    "status": "ACTIVE"
}, {
    "type": "C",
    "id": "C",
    "name": "C",
    "message": "OK",
    "updateDate": "2018-07-31T10:55:18.607Z",
    "createDate": "2018-07-31T08:01:37.181Z",
    "status": "ACTIVE"
}, {
    "type": "D",
    "id": "D",
    "name": "D",
    "message": "OK",
    "updateDate": "2018-07-31T10:55:20.877Z",
    "createDate": "2018-07-31T08:01:38.185Z",
    "status": "ACTIVE"
}, {
    "type": "E",
    "id": "E",
    "name": "E",
    "message": "OK",
    "updateDate": "2018-07-31T10:55:18.615Z",
    "createDate": "2018-07-31T08:01:44.207Z",
    "status": "ACTIVE"
}, {
    "type": "F",
    "id": "F",
    "name": "F",
    "message": "OK",
    "updateDate": "2018-07-31T10:55:19.131Z",
    "createDate": "2018-07-31T08:01:44.207Z",
    "status": "ACTIVE"
}, {
    "type": "G",
    "id": "G",
    "name": "G",
    "message": "OK",
    "updateDate": "2018-07-31T10:55:18.326Z",
    "createDate": "2018-07-31T08:01:46.212Z",
    "status": "ACTIVE"
}]

To display the name and the status I use the following commands, where $input holds the above JSON:

output=$(jq '.[] | "\(.name) \(.status)"' <<< "$input")
echo $output

This produces:

"A: ACTIVE" "B: ACTIVE" "C: ACTIVE" "D: ACTIVE" "E: ACTIVE" "F: ACTIVE" "G: ACTIVE"

How do I ensure each iteration produces one new line, like so:

      "A: ACTIVE"
      "B: ACTIVE"
      "C: ACTIVE" 
      "D: ACTIVE
      "E: ACTIVE"
      "F: ACTIVE"
      "G: ACTIVE"
peak
  • 105,803
  • 17
  • 152
  • 177
Oren Ashkenazy
  • 271
  • 6
  • 19
  • 2
    The output is already on a new line. It does not print on the same line as you claim – Inian Jul 31 '18 at 15:33
  • 4
    I wonder if you're running `foo=$(jq ...); echo $foo`. In that case, it's the lack of quotes used in your `echo` that makes everything display on one line; it would need to be `echo "$foo"` to display correctly. – Charles Duffy Jul 31 '18 at 16:28
  • 1
    ...were that the case, this question would be a duplicate of [I just assigned a variable, but `echo $variable` shows something else](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else). – Charles Duffy Jul 31 '18 at 16:29
  • 3
    The fact that you aren't showing us enough to reproduce this ourselves (since you don't show how you're actually displaying the output, which is pertinent to how it's formatted/rendered) means this question lacks a [mcve]. Ideally, we should be able to copy-and-paste content from your question without changes to get the exact same output, so there should be no speculation needed as to whether the bug is "real" or not. – Charles Duffy Jul 31 '18 at 16:31
  • The JSON format is actually an docker exec output. You're right, I do run foo=$(jq ...); echo $foo. Great catch, thanks! – Oren Ashkenazy Jul 31 '18 at 18:35

1 Answers1

1

[EDIT: This response has been revised in accordance with clarification of the question.]

Use:

echo "$output"

If your bash has mapfile, it might also be worthwhile considering creating output as a bash array:

mapfile -t output < <(jq  '.[] | "\(.name) \(.status)"' <<< "$input" )
printf "%s\n" "${output[@]}"
peak
  • 105,803
  • 17
  • 152
  • 177
  • 1
    I would expect that the OP is mishandling their shell, and losing the newlines that way (as by running `var=$(jq ...)`, and later `echo $var`; thus running afoul of [BashPitfalls #14](http://mywiki.wooledge.org/BashPitfalls#echo_.24foo)). – Charles Duffy Jul 31 '18 at 16:29
  • @Isaac - The workaround explicitly includes -r already. You might want to delete your comment to avoid confusion. – peak Jul 31 '18 at 19:36