我正在使用JQPlay播放格式。我不明白如何使用归约子结构。我想根据组织或父ID进行分组。
只需更新我的jqplay过滤器,但无法删除两个按ID分组的标签。
我在jqplay.org中使用以下语法。也可以请您提出如何在管道符号后调试任何内容的建议。
.items | {"org" : map( {id : .org, orgProperties : [{"properties" : {"methodId" : [{"id" : .methodId}]}}]})| group_by(.id) | map( reduce .[] as $x (.[0]|{}; .orgProperties+= ($x | .orgProperties)))}
{
"items": [
{
"org": "750141",
"methodId": "1-10F7IAK7"
},
{
"org": "750141",
"methodId": "1-10TP18L0"
},
{
"org": "750142",
"methodId": "1-10TP18L1"
}
]
}
{
"org": [
{
"orgProperties": [
{
"properties": {
"methodId": [
{
"id": "1-10F7IAK7"
}
]
}
},
{
"properties": {
"methodId": [
{
"id": "1-10TP18L0"
}
]
}
}
]
},
{
"orgProperties": [
{
"properties": {
"methodId": [
{
"id": "1-10TP18L1"
}
]
}
}
]
}
]
}
{
"org": [
{
"id": "750141",
"orgProperties": [
{
"properties": {
"methodId": [
{
"id": "1-10F7IAK7"
},
{
"id": "1-10TP18L0"
}
]
}
}
]
},
{
"id": "750142",
"orgProperties": [
{
"properties": {
"methodId": [
{
"id": "1-10TP18L1"
}
]
}
}
]
}
]
}
您需要执行以下操作。您需要先group_by()
进行处理
jq '.items | {org: group_by(.org) | map({id: .[0].org, orgProperties: [{properties: { methodId: map({id: .methodId}) }}]})} ' input.json
我认为没有办法提高调试的详细程度,jq
除非可能修改代码以添加自己的调试语句并运行自定义构建。
我个人一次从一个组件构建过滤器,观察其输出并在此之上进行操作。分解以上功能
'.items | {org: ..
在过滤器的一部分之后,JSON会完全重建。后续部分的结果形成了现在在顶层以下"org"
的预期输出。group_by(.org)
,您将得到一个包含两个数组条目的结果,一个包含2个对象(id 750141
),另一个包含单个对象(id 750142
)。map(..)
,在上一步返回的对象列表中运行代码我们只需要在最终结果中使用唯一的键名称,因此我们只.[0].org
在第一个数组中使用。即使您有多个重复的键名称,此功能也可以使用。暂停并查看输出,直到这一点
{
"org": [
{
"id": "750141"
},
{
"id": "750142"
}
]
}
现在,构建其余的输出,orgProperties: [{properties: { methodId: ...
并使用这些输出创建子节点
"orgProperties": [
{
"properties": {
"methodId":
创建最终的子数组,map({id: .methodId})
以创建带有ID列表的键值对
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句