json - jq:将数据合并到树中
问题描述
我的数据包含categories
包含elements
. 以 .csv 格式表示,有关类别的数据与每个元素重复。我想通过以 .json 树格式存储数据来避免这种情况。
我该如何整合这一系列elements
......</p>
[
{
"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
}
]
}
]
我希望这是一件相当微不足道的jq
事情——否则我会写一些更重量级的东西。
解决方案
按类别和评论对对象进行分组,然后根据需要映射分组:
group_by({ category, comment }) | map({
category: .[0].category,
comment: .[0].comment,
elements: map({
foo: .element_foo,
bar: .element_bar
})
})
推荐阅读
- sql - 如何引用确切的对象
- javascript - 反应形式模式 IE11
- elasticsearch - ElasticSearch Painless:在 for 循环中使用向量函数错误
- wordpress - Elementor Post Widget - 从另一个(多站点)博客 ID 获取帖子
- database-design - 医疗库存管理数据库(MLD)的设计
- flutter - 如何在具有空安全性的颤动中创建构造函数
- javascript - jQuery 使用数据中的元素创建 div
- python - JSON 和 Heroku 的问题
- signalr-hub - 多租户应用程序中的 SignalR - 包装 IHubContext<>
- android-studio - 当我覆盖 FirebaseStorage 中的图像时,无论如何我都会得到旧图像