JQ how to print newline and not newline character from json value

jjgong picture jjgong · Sep 22, 2015 · Viewed 25.8k times · Source

I have some logs that output information in JSON. This is for collection to elasticsearch.

Some testers and operations people want to be able to read logs on the servers.

Here is some example JSON:

{
"@timestamp": "2015-09-22T10:54:35.449+02:00",
"@version": 1,
"HOSTNAME": "server1.example",
"level": "WARN",
"level_value": 30000,
"logger_name": "server1.example.adapter",
"message": "message"
"stack_trace": "ERROR LALALLA\nERROR INFO NANANAN\nSOME MORE ERROR INFO\nBABABABABABBA BABABABA ABABBABAA BABABABAB\n"
}

And so on.

Is it possible to make Jq print newline instead of the \n char?

Answer

user3899165 picture user3899165 · Sep 22, 2015

Sure! Using the -r option, jq will print string contents directly to the terminal instead of as JSON escaped strings.

jq -r '.stack_trace'