在查询某些文档时,我想在层次结构中选择项目,但在结构中保留父名称。例如,在查询aws数据库成员时,我可以使用:
DBClusters[].DBClusterMembers[].[DBInstanceIdentifier,IsClusterWriter]
来自:
{
"DBClusters": [
{
"DatabaseName": "bazbar",
"DBClusterMembers": [
{
"IsClusterWriter": false,
"DBClusterParameterGroupStatus": "in-sync",
"PromotionTier": 2,
"DBInstanceIdentifier": "foobar"
},
...
要:
[
[
"foobar",
false
],
...
但我怎样才能得到以下回应:
{
"bazbar": [
[
"foobar",
false
],
...
即。对于每个集群,提取DatabaseName
作为成员列表的密钥名称?
答案 0 :(得分:1)
假设以下样本数据集...
{
"dbclusters": [
{
"databasename": "alpha",
"dbclustermembers": [
{
"isclusterwriter": false,
"dbinstanceidentifier": "foobar"
}
,{
"isclusterwriter": true,
"dbinstanceidentifier": "doobar"
}
]
}
,{
"databasename": "bravo",
"dbclustermembers": [
{
"isclusterwriter": false,
"dbinstanceidentifier": "foobar"
}
]
}
,{
"databasename": "charlie",
"dbclustermembers": [
{
"isclusterwriter": false,
"dbinstanceidentifier": "foobar"
}
]
}
]
}
...以下jmespath查询...
@.dbclusters[].{"databasename":databasename
,"dbclustermembers":[dbclustermembers[*].isclusterwriter
,dbclustermembers[*].dbinstanceidentifier][]
}
...产生以下结果...
[
{
"databasename": "alpha",
"dbclustermembers": [
false,
true,
"foobar",
"doobar"
]
},
{
"databasename": "bravo",
"dbclustermembers": [
false,
"foobar"
]
},
{
"databasename": "charlie",
"dbclustermembers": [
false,
"foobar"
]
}
]