如何通过jq合并(平均/总和).json中的重复对象?

时间:2018-04-10 17:35:36

标签: json bash jq

我试图摆脱有时出现在我输入中的重复项 以下是数据样本:

{
  "some": "very",
  "random": 0.228,
  "stuff": 31337,
  "people": [
    {
      "name": "carl",
      "height": "180cm",
      "wifes": 1,
      "sons": 0
    },
    {
      "name": "charlie",
      "height": "166cm",
      "wifes": 0,
      "sons": 2
    },
    {
      "name": "carl",
      "height": "180cm",
      "wifes": 1,
      "sons": 1
    },
    {
      "name": "carl",
      "height": "195cm",
      "wifes": 1.95,
      "sons": 12
    }
  ]
}

有很多卡尔,但其中只有2个是重复的 - 名字:carl&高度:180厘米
例如,我需要平均他的妻子的数量,并总结他的儿子 这是预期的结果:

[
  {
    "name": "carl",
    "height": "180cm",
    "wifes": 1,
    "sons": 3
  },
  {
    "name": "charlie",
    "height": "166cm",
    "wifes": 0,
    "sons": 2
  },
  {
    "name": "carl",
    "height": "195cm",
    "wifes": 1.95,
    "sons": 12
  }
]

我尝试使用'添加'并且'减少'但我在jq><

中非常新手

1 个答案:

答案 0 :(得分:1)

分组可以通过group_by/1使用[.name,。height]作为分组标准来实现:

def sum(f): map(f) | add;

def average(f): sum(f) / length;

def consolidate:
  .[0]
  + {wifes: average(.wifes)}
  + {sons:  sum(.sons) } ;

.people
| group_by([.name,.height])
| map(consolidate)

输出

输出符合给定的描述性要求:

[
  {
    "name": "carl",
    "height": "180cm",
    "wifes": 1,
    "sons": 1
  },
  {
    "name": "carl",
    "height": "195cm",
    "wifes": 1.95,
    "sons": 12
  },
  {
    "name": "charlie",
    "height": "166cm",
    "wifes": 0,
    "sons": 2
  }
]