JQ how to print a newline character, not a newline character from json value

I have several logs that output information in JSON. This is for the collection for elastics search.

Some testers and operators want them to read logs on servers.

Here is a JSON example:

{
"@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"
}

Etc.

Is it possible to make Jq print a new line instead of \ n char?

+4
source share
3 answers

Input, as originally presented, is not entirely correct JSON, and it does not exactly determine what the desired result is, but the following may be of interest. It is written for the current version of jq (version 1.5), but can be easily 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
"
+2
source

! -r, jq , JSON.

jq -r '.stack_trace'
+14

jq , "" ( "un-json-ify" ) jq sed:

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

(\t JSON), :

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

, stack_trace Java ( , ), stacktrace Java <tab>at<space>.

: , , , JSON, \\n, ", " n". , , , , . sed , ( ).

+1
source

Source: https://habr.com/ru/post/1608469/


All Articles