JQ如何从json值打印换行符而不是换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32715008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:03:40  来源:igfitidea点击:

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

jsonnewlinejq

提问by jjgong

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

我有一些以 JSON 格式输出信息的日志。这是为了收集到elasticsearch。

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

一些测试人员和运维人员希望能够读取服务器上的日志。

Here is some example JSON:

这是一些示例 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?

是否可以让 Jq 打印换行符而不是 \n 字符?

采纳答案by peak

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:

最初给出的输入不是非常有效的 JSON,并且不清楚所需的输出是什么,但以下内容可能会引起人们的兴趣。它是为当前版本的 jq(1.5 版)编写的,但可以很容易地适应 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
"

回答by

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

当然!使用该-r选项,jq 会将字符串内容直接打印到终端,而不是作为 JSON 转义字符串。

jq -r '.stack_trace'

回答by Piotr Findeisen

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

除非您限制使用jqonly,否则您可以“修复”(或实际上是“un-json-ify”)jq输出sed

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

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

如果您碰巧在输入中也有选项卡(\t在 JSON 中),则:

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

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

如果您stack_trace是由 Java 生成的(您没有说明日志的来源是什么),这将特别方便,因为 Java 堆栈跟踪行以<tab>at<space>.

Warning: naturally, this is not correct, in a sense that JSON input containing \\nwill 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 sedpatterns can be further improved to take care for this (at the cost of readability).

警告:当然,这是不正确的,从某种意义上说,包含 JSON 输入\\n将导致 "" 输出,但它应该导致 "n" 输出。虽然不正确,但对于人类窥视数据肯定足够了。该sed图案可以进一步改善照顾此(在可读性的费用)。