【发布时间】:2018-04-13 05:51:54
【问题描述】:
我正在尝试从通过 --arg 参数传递给 jq 的一堆值生成 JSON 文件。结果格式正确,但内容却出乎意料:
$ jq --arg value_a0 1.1.1.1 --arg value_b0 81 --arg value_a1 2.2.2.2 --arg value_b1 82 --arg value_a2 3.3.3.3 --arg value_b2 83 '. | .nodes=[ .ip=$value_a0 | .port=$value_b0, .ip=$value_a1 | .port=$value_b1, .ip=$value_a2 | .port=$value_b2 ]' <<<'{}'
{
"nodes": [
{
"ip": "1.1.1.1",
"port": "83"
},
{
"ip": "3.3.3.3",
"port": "83"
},
{
"ip": "2.2.2.2",
"port": "83"
},
{
"ip": "3.3.3.3",
"port": "83"
}
]
}
我希望得到:
{
"nodes": [
{
"ip": "1.1.1.1",
"port": "81"
},
{
"ip": "2.2.2.2",
"port": "82"
},
{
"ip": "3.3.3.3",
"port": "83"
}
]
}
我希望 .nodes=[] 只是总结用逗号分隔的操作结果,但我显然遗漏了一些东西。 :(
【问题讨论】:
-
好吧,看了一会儿,我现在看到了我的方式的错误。 :) 我在想“,”操作数分隔
| 来自 | ;但实际上它将 与 分开。叹。 :)