Reputation: 30166
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
Upvotes: 1
Views: 3372
Reputation: 1
I recommend this: I just splitted the output string in parts and added an extra empty filter "" to get an empty line.
jq -r '.records[] | "id: \(.id)", "name: \(.name)", ""' afile.json
Upvotes: 0
Reputation: 116690
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.
Upvotes: 2
Reputation: 43944
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.
Upvotes: 2