I have the following file
[
{
"id": 1,
"name": "Arthur",
"age": "21"
},
{
"id": 2,
"name": "Richard",
"age": "32"
}
]
To display login and id together, I am using the following command
$ jq '.[] | .name' test
"Arthur"
"Richard"
But when I put it in a shell script and try to assign it to a variable then the whole output is displayed on a single line like below
#!/bin/bash
names=$(jq '.[] | .name' test)
echo $names
$ ./script.sh
"Arthur" "Richard"
I want to break at every iteration similar to how it works on the command line.
Couple of issues in the information you have provided. The
jq
filter.[] | .login, .id
will not produce the output as you claimed onjq-1.5
. For your originalJSON
It will produce four lines of output as,
If you are interested in storing them side by side, you need to do variable interpolation as
And if you feel your output stored in a variable is not working. It is probably because of lack of double-quotes when you tried to print the variable in the shell.
This way the embedded new lines in the command output are not swallowed by the shell.
For you updated
JSON
(totally new one compared to old one), all you need to do is