我有以下 json 文件:
common.json
{
"common": {
"Hello": "ABC"
},
"east": {
"Direction": "East"
},
"west": {
"Direction": "West"
}
}
pi.json
{
"common": {
"MyUri": "www.myuri.com"
},
"east": {
"App-Env": "my-pi",
"SentinelValue": 1713311902
},
"west": {
"App-Env": "my-piw",
"SentinelValue": 1713311902
}
}
基本上我想创建一个新的json文件,其中包含common.json的“east”键+common.json的“common”键+pi.json的“common”键+pi.json的“east”键的所有内容被移至一个 json 文件。
我的以下脚本没有按照我想象的方式工作:
jq_input_files=("common.json" "pi.json")
jq --slurp 'map({
((.common | keys_unsorted[])): .common[],
((.east | keys_unsorted[])): .east[]
}) | add' "${jq_input_files[@]}" > "test.json"
该脚本中 test.json 的内容是:
{
"MyUri": "www.myuri.com",
"App-Env": my-pi,
"SentinelValue": 1713311902
}
我想要的 test.json 输出是:
{
"MyUri": "www.myuri.com",
"App-Env": 1713311902,
"SentinelValue": 1713311902,
"Hello": "ABC",
"Direction": "East"
}
我无法弄清楚的两个主要问题:
您
--slurp
将两个文件放入一个数组中,但仍然不单独寻址它们。
来自 common.json 的“east”键 + common.json 的“common”键 + pi.json 的“common”键 + pi.json 的“east”键
您可以通过
first
和 last
(或 .[0]
和 .[1]
)来称呼他们:
first.east + first.common + last.common + last.east
如果添加顺序并不重要(如果发生冲突,后者会覆盖前者),您可以坚持使用
map
并将所有内容放在一起。最后,使用 add
将它们汇总为一个对象:
map(.east, .common) | add