jq合并对象内部的数组 [英] jq merge arrays inside object

查看:67
本文介绍了jq合并对象内部的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个物体

{
    "a": [
        "1-1",
        "1-2"
    ],
    ...
}

{ 
    "a": [
        "2-1",
        "2-2",
        "2-3"
    ],
    ...
}

两个对象中都有其他键,但我不在乎它们.

there are other keys in both objects, but I don't care for them.

我想要得到的是将"a"的元素连接在一起的对象:

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.

如何使用jq做到这一点?

推荐答案

一种方法是使用-s命令行选项:

One way would be to use the -s command-line option:

jq -s '.[1].a as $a1 | .[0] | (.a += $a1)'

由于您不关心非a键,因此可以选择:

Since you don't care about the non-a keys, an alternative would be:

jq -n '{a: (input.a + input.a)}' 

请注意上一行中的-n选项.

Notice the -n option in the line above.

这篇关于jq合并对象内部的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆