jq & bash:从变量生成 JSON 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22434290/
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
jq & bash: make JSON array from variable
提问by Andrey Regentov
I'm using jqto form a JSON in bash from variable values.
我正在使用jq从变量值在 bash 中形成一个 JSON。
Got how to make plain variables
了解了如何制作普通变量
$ VAR="one two three"
$ jq -n "{var:\"$VAR\"}"
{
"var": "one two three"
}
But can't make arrays yet. I have
但是还不能做数组。我有
$ echo $ARR
one
two
three
and want to get something like
并且想要得到类似的东西
{
"arr": ["one", "two", "three"]
}
I only manage to get garbled output like
我只能得到像这样的乱码输出
$ jq -n "{arr: [\"$ARR\"]}"
{
"arr": [
"one\ntwo\nthree"
]
}
How to form JSON array in a correct way? Can jqever do that?
如何以正确的方式形成JSON数组?能jq做到吗?
EDIT: Question was asked when there was only jq 1.3. Now, in jq 1.4, it is possible to do straightly what I asked for, like @JeffMercado and @peak suggested, upvote for them. Won't undo acceptance of @jbr 's answer though.
编辑:只有 jq 1.3 时才提出问题。现在,在 jq 1.4 中,可以直接按照我的要求做,就像 @JeffMercado 和 @peak 建议的那样,为他们点赞。不过不会撤消对@jbr 的回答的接受。
采纳答案by jbr
jqis doing exactly what you tell it to do. So if you want it to print an object with an array containing "one", "two", "three" then you have to generate it.jqis not a program for generating JSON, but a tool for querying it. What you are doing with the -nswitch is just using it as a pretty printer.
jq正在做你告诉它做的事情。所以如果你想让它打印一个包含“一”、“二”、“三”的数组的对象,那么你必须生成它。jq不是生成 JSON 的程序,而是查询它的工具。您对-n交换机所做的只是将其用作漂亮的打印机。
VAR="one two three"
VAR=$(echo $VAR | sed -e 's/\(\w*\)/,""/g' | cut -d , -f 2-)
echo "{var: [$VAR]}"
Update
更新
As Bryanand others mention below it is indeed possible to generate JSON with jq, and from version 1.4, it's even possible to do what the OP ask directly, see for example Jeff's answer.
正如Bryan和其他人在下面提到的,确实可以使用 jq 生成 JSON,从 1.4 版开始,甚至可以直接执行 OP 要求的操作,例如参见Jeff 的回答。
回答by user2259432
In jq 1.3 and up you can use the --arg VARIABLE VALUEcommand-line option:
在 jq 1.3 及更高版本中,您可以使用--arg VARIABLE VALUE命令行选项:
jq -n --arg v "$VAR" '{"foo": $v}'
I.e., --argsets a variable to the given value so you can then use $varnamein your jq program, and now you don't have to use shell variable interpolation into your jq program.
即,--arg将变量设置为给定值,以便您可以$varname在 jq 程序中使用,现在您不必在 jq 程序中使用 shell 变量插值。
EDIT: From jq 1.5 and up, you can use --argjson to pass in an array directly, e.g.
编辑:从 jq 1.5 及更高版本,您可以使用 --argjson 直接传入数组,例如
jq -n --argjson v '[1,2,3]' '{"foo": $v}'
回答by Jeff Mercado
回答by peak
The original posting in this thread mentions VAR="one two three". For such a variable, a very simple solution in jq 1.4 is as follows:
此线程中的原始帖子提到 VAR="一二三"。对于这样的变量,jq 1.4中一个非常简单的解决方法如下:
$ jq -n -c -M --arg s "$VAR" '{var: ($s|split(" "))}'
{"var":["one","two","three"]}
(This assumes a Linux or Mac shell or similar, and works with jq 1.4.)
(这假设是 Linux 或 Mac shell 或类似的,并且适用于 jq 1.4。)
If the separator character is a newline, then consider this typescript:
如果分隔符是换行符,请考虑以下打字稿:
$ s=$(printf "a\nb\nc\n")
$ jq -n -c -M --arg var "$s" '{"var": ($var|split("\n"))}'
{"var":["a","b","c"]}
Notice that the JSON string for the newline character is "\n"(not "\\n").
请注意,换行符的 JSON 字符串是"\n"(not "\\n")。
A third case of interest is when VAR is a bash array. In this case, it may be acceptable to pass in the items using "${VAR[@]}", but in general, it must be remembered that the "--arg name value" option is intended only for passing in strings.
第三种感兴趣的情况是 VAR 是一个 bash 数组。在这种情况下,使用“${VAR[@]}”传递项目可能是可以接受的,但一般来说,必须记住“--arg name value”选项仅用于传递字符串。
In jq>1.4, exported shell variables can be passed in using the "env" object, as documented in the online jq manual page.
在 jq>1.4 中,可以使用“env”对象传入导出的 shell 变量,如在线 jq 手册页中所述。
回答by ceving
First you have to use -Rto read the raw lines and next you have to slurp all values with -s:
首先,您必须使用-R读取原始行,然后您必须使用以下命令读取所有值-s:
$ echo -e "one\ntwo\nthree" | jq -R . | jq -s '{"arr": .}'
{
"arr": [
"one",
"two",
"three"
]
}
回答by ashanbrown
This worked for me in jq 1.6:
这在 jq 1.6 中对我有用:
$ jq -nc '$ARGS.positional' --args 1 2 3
["1","2","3"]
For this specific use-case, you could use an array and write:
对于此特定用例,您可以使用数组并编写:
$ VAR=(one two three)
$ jq -nc '{var: $ARGS.positional}' --args ${VAR[@]}
{"var":["one","two","three"]}
You could also use a string like this:
你也可以使用这样的字符串:
$ VAR="one two three"
$ jq -nc '{var: ($ARGS.positional[0] | split(" "))}' --args $VAR
回答by alexglue
You can use this syntax:
您可以使用以下语法:
jq --arg keyName "MyVal" '.[]."\($keyName)"[] ./JSONFile.js
for input data like
对于输入数据,如
[{"MyVal":"Value1","MyVal":Value2,"NotMyVal":"Value3"}];
to get parsed data from JSON with variables.
使用变量从 JSON 获取解析数据。
This example will print:
此示例将打印:
"Value1"
"Value2"

