I have two json files, orig.json and patch.json, which have similar formats.
orig.json:
{
"a": {
"a1": "original a1",
"a2": "original a2",
"list": ["baz", "bar"]
},
"b": "original value B"
}
patch.json:
{
"a": {
"a1": "patch a1",
"list": ["foo"]
},
"c": "original c"
}
Currently I am using jq
to merge them recursively. However, jq's default behavior for lists is just reassignment. Example output from jq using $ jq -s '.[0] * .[1]' orig.json patch.json
:
{
"a": {
"a1": "patch a1",
"a2": "original a2",
"list": [
"foo"
]
},
"b": "original value B",
"c": "original c"
}
Note that a.list
is now equal to patch.json's a.list
. I want The new a.list
to be orig.json's list and patch.json's lists merged. In other words, I want a.list
to equal ["baz", "bar", "foo"]
.
Is there a way I can easily do this with jq, perhaps by overriding the default merge strategy for arrays?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…