在带有此查询的 JMESPath 中:
people[].{"index":@.index,"name":name, "state":state.name}
在此示例数据上:
{
"people": [
{
"name": "a",
"state": {"name": "up"}
},
{
"name": "b",
"state": {"name": "down"}
},
{
"name": "c",
"state": {"name": "up"}
}
]
}
我得到:
[
{
"index": null,
"name": "a",
"state": "up"
},
{
"index": null,
"name": "b",
"state": "down"
},
{
"index": null,
"name": "c",
"state": "up"
}
]
如何让 index 属性实际拥有数组的索引?我意识到
@.index
不是正确的语法,但无法找到返回索引的函数。有没有办法包含当前数组索引?
最佳答案
这不完全是您要求的表格,但我为您提供了一个可能的答案:people[].{"name":name, "state":state.name} | merge({count: length(@)}, @[*])
这个请求给出了这个结果:
{
"0": {
"name": "a",
"state": "up"
},
"1": {
"name": "b",
"state": "down"
},
"2": {
"name": "c",
"state": "up"
},
"count": 3
}
所以这个对象的每个属性都有一个索引,除了最后一个
count
它只是引用属性的数量,所以如果你想用循环浏览对象的属性,例如你可以这样做,因为你知道属性count
给出要浏览的属性数。
关于indexof - JMESPath 当前数组索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51526317/