bash 在bash中将行转换为json

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26287130/
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-18 11:30:43  来源:igfitidea点击:

converting lines to json in bash

bashjq

提问by jcalfee314

I would like to convert a list into JSON array. I'm looking at jqfor this but the examples are mostly about parsing JSON (not creating it). It would be nice to know proper escaping will occur. My list is single line elements so the new line will probably be the best delimiter.

我想将列表转换为 JSON 数组。我正在寻找jq这个,但示例主要是关于解析 JSON(而不是创建它)。很高兴知道会发生正确的转义。我的列表是单行元素,所以新行可能是最好的分隔符。

回答by chbrown

I was also trying to convert a bunch of lines into a JSON array, and was at a standstill until I realized that -swas the only way I could handle more than one line at a time in the jqexpression, even if that meant I'd have to parse the newlines manually.

我还试图将一堆行转换为 JSON 数组,并且一直处于停滞状态,直到我意识到这-s是我可以在jq表达式中一次处理多行的唯一方法,即使这意味着我有手动解析换行符。

jq -R -s -c 'split("\n")' < just_lines.txt
  • -Rto read raw input
  • -sto read all input as a single string
  • -cto not pretty print the output
  • -R读取原始输入
  • -s将所有输入读取为单个字符串
  • -c不漂亮地打印输出

Easy peasy.

十分简单。

Edit: I'm on jq≥ 1.4, which is apparently when the splitbuilt-in was introduced.

编辑:我在jq≥ 1.4,这显然是split在引入内置时。

回答by nisetama

You can also use jq -R .to format each line as a JSON string and then jq -s(--slurp) to create an array for the input lines after parsing them as JSON:

您还可以使用jq -R .将每一行格式化为 JSON 字符串,然后jq -s( --slurp) 在将它们解析为 JSON 后为输入行创建一个数组:

$ printf %s\n aa bb|jq -R .|jq -s .
[
  "aa",
  "bb"
]

The method in chbrown's answer adds an empty element to the end if the input ends with a linefeed, but you can use printf %s "$(cat)"to remove trailing linefeeds:

如果输入以换行符结尾,则 chbrown 的答案中的方法会在末尾添加一个空元素,但您可以使用printf %s "$(cat)"删除尾随换行符:

$ printf %s\n aa bb|jq -R -s 'split("\n")'
[
  "aa",
  "bb",
  ""
]
$ printf %s\n aa bb|printf %s "$(cat)"|jq -R -s 'split("\n")'
[
  "aa",
  "bb"
]

If the input lines don't contain ASCII control characters (which have to be escaped in strings in valid JSON), you can use sed:

如果输入行不包含 ASCII 控制字符(必须在有效 JSON 的字符串中转义),您可以使用sed

$ printf %s\n aa bb|sed 's/["\]/\&/g;s/.*/"&"/;1s/^/[/;$s/$/]/;$!s/$/,/'
["aa",
"bb"]

回答by Sridhar Sarnobat

--raw-input, then --slurp

--raw-input,然后 --slurp

Just summarizing what the others have said in a hopefully quicker to understand form:

只是以一种希望更快理解的形式总结其他人所说的话:

cat /etc/hosts  | jq  --raw-input .  | jq --slurp .

will return you:

会给你回报:

[
  "fe00::0 ip6-localnet",
  "ff00::0 ip6-mcastprefix",
  "ff02::1 ip6-allnodes",
  "ff02::2 ip6-allrouters"
]

Explanation

解释

 --raw-input/-R:

       Don′t parse the input as JSON. Instead, each line of text is passed
       to  the  filter  as  a  string.  If combined with --slurp, then the
       entire input is passed to the filter as a single long string.

 --slurp/-s:

       Instead of running the filter for each JSON object  in  the  input,
       read  the entire input stream into a large array and run the filter
       just once.

回答by peak

Update: If your jq has inputsyou can simply write:

更新:如果你的 jq 有inputs你可以简单地写:

jq -nR [inputs] /etc/hosts 

to produce a JSON array of strings. This avoids having to read the text file as a whole.

生成一个 JSON 字符串数组。这避免了必须阅读整个文本文件。

回答by Corey Woodfield

I found in the man page for jq and through experimentation what seems to me to be a simpler answer.

我在 jq 的手册页中发现并通过实验在我看来是一个更简单的答案。

$ cat test_file.txt | jq -Rsc '. / "\n" - [""]'
["aa","bb"]

The -R is to read without trying to parse json, the -s says to read all of the input as one string, and the -c is for one-line output - not necessary, but it's what I was looking for.

-R 是在不尝试解析 json 的情况下读取,-s 表示将所有输入读取为一个字符串,而 -c 用于单行输出 - 不是必需的,但这是我正在寻找的。

Then in the string I pass to jq, the '.' says take the input as it is. The '/ \n' says to divide the string (split it) on newlines. The '- [""]' says to remove from the resulting array any empty strings (resulting from an extra newline at the end).

然后在我传递给 jq 的字符串中,'.' 说按原样接受输入。'/ \n' 表示在换行符上分割字符串(拆分)。'- [""]' 表示从结果数组中删除任何空字符串(由末尾的额外换行符产生)。

It's one line and without any complicated constructs, using just simple built in jq features.

这是一行,没有任何复杂的结构,仅使用简单的内置 jq 功能。