我有以下两个json文件。
file1.json
{"field1":{"item1":0,"array1":[0,0,0],"array2":[0,0],"array3":[0]},"field2":{"dummy":0}}
file2.json
{"field1":{"item1":1,"array1":[1,1,1],"array2":[1,1],"array3":[1]},"field2":{"dummy":0}}
我想获得这个:
{"field1":{"item1":0,"array1":[0,0,0,1,1,1],"array2":[0,0,1,1],"array3":[0,1]},"field2":{"dummy":0}}
我能够通过数组手动数组,但我更喜欢一种方法为field1的每个数组自动执行。有人给我一个提示吗?
非常感谢你的帮助!
答案 0 :(得分:1)
以下假设file1.json和file2.json包含有效的JSON。您可以通过运行:jq -n -f fileN.json
def extend_arrays(o):
reduce keys[] as $k
(.; if (.[$k]|type) == "array" and (o[$k]|type == "array")
then .[$k] += o[$k] else . end);
.field1 |= extend_arrays($second|.field1)
在program.jq文件中使用这些行,调用:
jq -c --argfile second file2.json -f program.jq file1.json
产生所需的输出:
{"field1":{"item1":0,"array1":[0,0,0,1,1,1],"array2":[0,0,1,1],"array3":[0,1]},"field2":{"dummy":0}}