JQ how to merge multiple objects in an array into

2019-07-27 11:52发布

问题:

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: .}