jq merge arrays inside object [closed]

假装没事ソ 提交于 2021-02-10 15:11:44

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!