Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

json

newline

jq

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

jq -r '.stack_trace'

Unless you're constraint to use jq only, you can "fix" (or actually "un-json-ify") jq output with sed:

cat the-input | jq . | sed 's/\\n/\n/g'

If you happen to have tabs in the input as well (\t in JSON), then:

cat the-input | jq . | sed 's/\\n/\n/g; s/\\t/\t/g'

This would be especially handy if your stack_trace was generated by Java (you didn't tell what is the source of the logs), as the Java stacktrace lines begin with <tab>at<space>.

Warning: naturally, this is not correct, in a sense that JSON input containing \\n will result in a "" output, however it should result in "n" output. While not correct, it's certainly sufficient for peeking at the data by humans. The sed patterns can be further improved to take care for this (at the cost of readability).


The input as originally given isn't quite valid JSON, and it's not clear precisely what the desired output is, but the following might be of interest. It is written for the current version of jq (version 1.5) but could easily be adapted for jq 1.4:

def json2qjson:
  def pp: if type == "string" then "\"\(.)\""  else . end;
  . as $in
  | foreach keys[] as $k (null; null; "\"\($k)\": \($in[$k] | pp)" ) ;


def data: {
  "@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"
};

data | json2qjson

Output:

$ jq -rnf json2qjson.jq
"@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
ERROR INFO NANANAN
SOME MORE ERROR INFO
BABABABABABBA BABABABA ABABBABAA BABABABAB
"