Json 使用jq将子文件合并到主文件中
关于合并JSON文档,有很多与jq相关的问题,但它们似乎都是关于合并具有相同结构的文档 我的问题不同,因为我需要将子文档合并到父文档中,其中子文档和父文档具有不同的结构 父文档如下所示:Json 使用jq将子文件合并到主文件中,json,merge,jq,Json,Merge,Jq,关于合并JSON文档,有很多与jq相关的问题,但它们似乎都是关于合并具有相同结构的文档 我的问题不同,因为我需要将子文档合并到父文档中,其中子文档和父文档具有不同的结构 父文档如下所示: { "resources": [ { "name": "child0", "properties": { "template": {} } }, { "name": "child1", "properties
{
"resources": [
{
"name": "child0",
"properties": {
"template": {}
}
},
{
"name": "child1",
"properties": {
"template": {}
}
},
{
"name": "child2",
"properties": {
"template": {}
}
}
]
}
我需要做的是将子文档合并到.resources[].properties.template
,这样第一个文件位于.resources[0].properties.template
,第二个文件位于.resources[1].properties.template
等
请注意,子文档的结构在这里并不重要,因为我希望将它们全部合并
我实际上已经实现了这一点,但使用了多个命令:
jq'.resources[0].properties.template+=input'main.json child0.json>temp0.json
jq'.resources[1].properties.template+=input'temp0.json child1.json>temp1.json
jq'.resources[2].properties.template+=input'temp1.json child2.json>temp2.json
mv temp2.json merged.json
rm temp*.json
这很难看-我正在寻找一个可以合并到子文件中的
jq
命令。您已经接近了。假设resources
中的对象数等于子文档数,这应该可以做到:
jq '.resources[].properties.template |= . + input' parent children...
这太完美了,谢谢!我似乎真的因为某种原因与jq斗争:/@OguzIsmail-完美@谢谢,我不明白为什么
+=
不起作用