【发布时间】:2020-01-06 18:34:57
【问题描述】:
我正在使用 JQPlay 来播放该格式。我无法理解如何使用 reduce 来按子结构分组。我想根据组织或父 ID 进行分组。
只需更新我的 jqplay 过滤器,但无法删除两个标签以按 id 分组。
jq 播放语法-
我在 jqplay.org 中使用以下语法。也请您告知如何调试管道符号后的任何内容。
.items | {"org" : map( {id : .org, orgProperties : [{"properties" : {"methodId" : [{"id" : .methodId}]}}]})| group_by(.id) | map( reduce .[] as $x (.[0]|{}; .orgProperties+= ($x | .orgProperties)))}
输入 JSON
{
"items": [
{
"org": "750141",
"methodId": "1-10F7IAK7"
},
{
"org": "750141",
"methodId": "1-10TP18L0"
},
{
"org": "750142",
"methodId": "1-10TP18L1"
}
]
}
输出 JSON
{
"org": [
{
"orgProperties": [
{
"properties": {
"methodId": [
{
"id": "1-10F7IAK7"
}
]
}
},
{
"properties": {
"methodId": [
{
"id": "1-10TP18L0"
}
]
}
}
]
},
{
"orgProperties": [
{
"properties": {
"methodId": [
{
"id": "1-10TP18L1"
}
]
}
}
]
}
]
}
预期的 JSON 输出
{
"org": [
{
"id": "750141",
"orgProperties": [
{
"properties": {
"methodId": [
{
"id": "1-10F7IAK7"
},
{
"id": "1-10TP18L0"
}
]
}
}
]
},
{
"id": "750142",
"orgProperties": [
{
"properties": {
"methodId": [
{
"id": "1-10TP18L1"
}
]
}
}
]
}
]
}
【问题讨论】: