gpt4 book ai didi

arrays - jq:递归合并对象并连接数组

转载 作者:行者123 更新时间:2023-12-01 03:09:17 25 4
gpt4 key购买 nike

我有两个 json 文件,orig.json 和 patch.json,它们具有相似的格式。

原件.json:

{
"a": {
"a1": "original a1",
"a2": "original a2",
"list": ["baz", "bar"]
},
"b": "original value B"
}

补丁.json:
{
"a": {
"a1": "patch a1",
"list": ["foo"]
},
"c": "original c"
}

目前我正在使用 jq递归合并它们。但是,jq 对列表的默认行为只是重新分配。 jq 使用 $ jq -s '.[0] * .[1]' orig.json patch.json 的示例输出:
{
"a": {
"a1": "patch a1",
"a2": "original a2",
"list": [
"foo"
]
},
"b": "original value B",
"c": "original c"
}

请注意 a.list现在等于 patch.json 的 a.list .我要新款 a.list将 orig.json 的列表和 patch.json 的列表合并。换句话说,我想要 a.list等于 ["baz", "bar", "foo"] .

有没有一种方法可以用 jq 轻松地做到这一点,也许是通过覆盖数组的默认合并策略?

最佳答案

这是一个通用函数,它通过在同一位置连接数组来递归组合两个复合 JSON 实体:

# Recursively meld a and b,
# concatenating arrays and
# favoring b when there is a conflict
def meld(a; b):
a as $a | b as $b
| if ($a|type) == "object" and ($b|type) == "object"
then reduce ([$a,$b]|add|keys_unsorted[]) as $k ({};
.[$k] = meld( $a[$k]; $b[$k]) )
elif ($a|type) == "array" and ($b|type) == "array"
then $a+$b
elif $b == null then $a
else $b
end;

meld($orig; $patch) 的输出

将 $orig 设置为 orig.json 的内容和
$patch 设置为 patch.json 的内容:
{
"a": {
"a1": "patch a1",
"a2": "original a2",
"list": [
"baz",
"bar",
"foo"
]
},
"b": "original value B",
"c": "original c"
}

关于arrays - jq:递归合并对象并连接数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53661930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com