问题
I have two objects
{
"a": [
"1-1",
"1-2"
],
...
}
and
{
"a": [
"2-1",
"2-2",
"2-3"
],
...
}
there are other keys in both objects, but I don't care for them.
What I want to get is the object where elements of "a"
will be concatenated:
{
"a": [
"1-1",
"1-2",
"2-1",
"2-2",
"2-3"
],
...
}
other keys can be replaced/merged/doesn't matter.
How do I do this with jq
?
回答1:
One way would be to use the -s command-line option:
jq -s '.[1].a as $a1 | .[0] | (.a += $a1)'
Since you don't care about the non-a keys, an alternative would be:
jq -n '{a: (input.a + input.a)}'
Notice the -n
option in the line above.
来源:https://stackoverflow.com/questions/50569174/jq-merge-arrays-inside-object