如果 JQ 中存在,则对对象进行递归
Recurse for object if exists in JQ
我有以下结构:
{
"hits":
[
{
"_index": "main"
},
{
"_index": "main",
"accordions": [
{
"id": "1",
"accordionBody": "body1",
"accordionInnerButtonTexts": [
"button11",
"button12"
]
},
{
"id": "2",
"accordionBody": "body2",
"accordionInnerButtonTexts": [
"button21",
"button22"
]
}
]
}
]
}
我想进入这个结构:
{
"index": "main"
}
{
"index": "main",
"accordions":
[
{
"id": "1",
"accordionBody": "body1",
"accordionInnerButtonTexts": [
"button11",
"button12"
]
},
{
"id": "2",
"accordionBody": "body2",
"accordionInnerButtonTexts": [
"button21",
"button22"
]
}
]
}
这意味着我总是希望将 _index
-字段包含为 index
,并且我希望将整个 accordions
-列表包含在对象中。这是我的尝试:
.hits[] | {index: ._index, accordions: recurse(.accordions[]?)}
它没有产生我想要的:
{
"index": "main",
"accordions": {
"_index": "main"
}
}
{
"index": "main",
"accordions": {
"_index": "main",
"accordions": [
{
"id": "1",
"accordionBody": "body1",
"accordionInnerButtonTexts": [
"button11",
"button12"
]
},
{
"id": "2",
"accordionBody": "body2",
"accordionInnerButtonTexts": [
"button21",
"button22"
]
}
]
}
}
{
"index": "main",
"accordions": {
"id": "1",
"accordionBody": "body1",
"accordionInnerButtonTexts": [
"button11",
"button12"
]
}
}
{
"index": "main",
"accordions": {
"id": "2",
"accordionBody": "body2",
"accordionInnerButtonTexts": [
"button21",
"button22"
]
}
}
它似乎创建了一个由混合对象给出的所有不同排列的列表。这不是我想要的。什么是正确的 jq 命令,我的错误是什么?
所述问题不需要任何递归。使用您的尝试作为模型,实际上可以简单地写:
.hits[]
| {index: ._index}
+ (if has("accordions") then {accordions} else {} end)
或者,语义完全不同:
.hits[] | {index: ._index} + . | del(._index)
我有以下结构:
{
"hits":
[
{
"_index": "main"
},
{
"_index": "main",
"accordions": [
{
"id": "1",
"accordionBody": "body1",
"accordionInnerButtonTexts": [
"button11",
"button12"
]
},
{
"id": "2",
"accordionBody": "body2",
"accordionInnerButtonTexts": [
"button21",
"button22"
]
}
]
}
]
}
我想进入这个结构:
{
"index": "main"
}
{
"index": "main",
"accordions":
[
{
"id": "1",
"accordionBody": "body1",
"accordionInnerButtonTexts": [
"button11",
"button12"
]
},
{
"id": "2",
"accordionBody": "body2",
"accordionInnerButtonTexts": [
"button21",
"button22"
]
}
]
}
这意味着我总是希望将 _index
-字段包含为 index
,并且我希望将整个 accordions
-列表包含在对象中。这是我的尝试:
.hits[] | {index: ._index, accordions: recurse(.accordions[]?)}
它没有产生我想要的:
{
"index": "main",
"accordions": {
"_index": "main"
}
}
{
"index": "main",
"accordions": {
"_index": "main",
"accordions": [
{
"id": "1",
"accordionBody": "body1",
"accordionInnerButtonTexts": [
"button11",
"button12"
]
},
{
"id": "2",
"accordionBody": "body2",
"accordionInnerButtonTexts": [
"button21",
"button22"
]
}
]
}
}
{
"index": "main",
"accordions": {
"id": "1",
"accordionBody": "body1",
"accordionInnerButtonTexts": [
"button11",
"button12"
]
}
}
{
"index": "main",
"accordions": {
"id": "2",
"accordionBody": "body2",
"accordionInnerButtonTexts": [
"button21",
"button22"
]
}
}
它似乎创建了一个由混合对象给出的所有不同排列的列表。这不是我想要的。什么是正确的 jq 命令,我的错误是什么?
所述问题不需要任何递归。使用您的尝试作为模型,实际上可以简单地写:
.hits[]
| {index: ._index}
+ (if has("accordions") then {accordions} else {} end)
或者,语义完全不同:
.hits[] | {index: ._index} + . | del(._index)