Suppose I have a json like this:
{
"records": [
{
"id": 1,
"name": "hello"
},
{
"id": 2,
"name": "world"
}
]
}
I could do this:
$ jq -r '.records[] | "id: \(.id) name: \(.name)"' afile.json
id: 1 name: hello
id: 2 name: world
How can I insert new lines so that I could get this:
id: 1
name: hello
id: 2
name: world
CodePudding user response:
Another option is to use map()
combined with join("\n\n")
to insert an \n
between each iteration. This way there won't be a trailing newline:
jq -rj '.records | map("id: \(.id)\nname: \(.name)") | join("\n\n")' input.json
id: 1
name: hello
id: 2
name: world
--raw-output
/-r
:With this option, if the filter's result is a string then it will be written directly to standard output rather than being formatted as a JSON string with quotes. This can be useful for making jq filters talk to non-JSON-based systems.
--join-output
/-j
:Like
-r
but jq won't print a newline after each output.
CodePudding user response:
One of many ways would be simply to add \n
to the end of the string specification.
If the extra newline at the end is unwanted, you could, for example, use sed '$d'
to remove it.