JQ how to merge multiple objects in an array into one

好久不见. 提交于 2019-12-13 00:57:35

问题


A little more sophisticated as my question mentioned below. I learned to use arrays more, but it screws things up too.

Input:

{
  "a": [
    {
      "b": "c",
      "d": "e"
    },
    {
      "b": "f",
      "d": "g"
    }
  ],
  "h": [
    {
      "b": "c",
      "i": "j"
    },
    {
      "b": "f",
      "i": "k"
    }
  ]
}

desired output:

{
  "l": [
    {
      "b": "c",
      "d": "e",
      "i": "j"
    },
    {
      "b": "f",
      "d": "g",
      "i": "k"
    }
  ]
}

Things that I've tried, based up on JQ How to merge multiple objects into one

{ x: [ inputs | .a[] | { (.h[]): .i } ] | add}

回答1:


The key to a simple solution is transpose:

[.a, .h]
| transpose
| map(add)
| {l: .}


来源:https://stackoverflow.com/questions/54350532/jq-how-to-merge-multiple-objects-in-an-array-into-one

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