jq:将数据整合到树中

时间:2018-09-06 14:25:46

标签: json jq

我的数据由包含categories的{​​{1}}组成。以.csv格式表示,有关类别的数据与每个元素重复。我想通过以.json树格式存储数据来避免这种情况。

如何合并elements的数组…

elements

…进入包含[ { "category": "a", "comment": "the repetition, oh my", "element_foo": 1, "element_bar": 10 }, { "category": "a", "comment": "the repetition, oh my", "element_foo": 2, "element_bar": 20 } ], 数组的categories数组吗?

elements

我希望这对于[ { "category": "a", "comment": "the repetition, oh my", "elements": [ { "foo": 1, "bar": 10 }, { "foo": 2, "bar": 20 } ] } ] 来说是微不足道的-否则我会写一些重量级的东西。

1 个答案:

答案 0 :(得分:3)

按类别和注释对对象进行分组,然后根据需要映射分组:

group_by({ category, comment }) | map({
    category: .[0].category,
    comment: .[0].comment,
    elements: map({
        foo: .element_foo,
        bar: .element_bar
    })
})
相关问题