jmespath :select json 基于对象中其他(数组)元素的对象元素
jmespath :select json object element based on other (array) element in the object
我有这个JSON
{
"srv_config": [{
"name": "db1",
"servers": ["srv1", "srv2"],
"prop": [{"source":"aa"},"destination":"bb"},{"source":"cc"},"destination":"cc"},]
}, {
"name": "db2",
"servers": ["srv2", "srv2"],
"prop": [{"source":"dd"},"destination":"dd"},{"source":"ee"},"destination":"ee"},]
}
]
}
我尝试构建一个 JMESPath 表达式来 select prop
应用程序在主数组中的每个对象中,但基于 servers
元素中存在的字符串。
对select所有道具,我能做到:
*.props [*]
但是,只有当 srv1
在服务器列表中时,我才能添加条件 "select"?
您可以使用 contains
函数根据包含某些内容的数组进行过滤。
鉴于查询:
*[?contains(servers, `srv1`)].prop | [][]
这给了我们:
[
{
"source": "aa",
"destination": "bb"
},
{
"source": "cc",
"destination": "cc"
}
]
请注意,我在这里也使用了一些 flattening。
所有这些 运行 都是为了你的更正版本 JSON:
{
"srv_config":[
{
"name":"db1",
"servers":[
"srv1",
"srv2"
],
"prop":[
{
"source":"aa",
"destination":"bb"
},
{
"source":"cc",
"destination":"cc"
}
]
},
{
"name":"db2",
"servers":[
"srv2",
"srv2"
],
"prop":[
{
"source":"dd",
"destination":"dd"
},
{
"source":"ee",
"destination":"ee"
}
]
}
]
}
我有这个JSON
{
"srv_config": [{
"name": "db1",
"servers": ["srv1", "srv2"],
"prop": [{"source":"aa"},"destination":"bb"},{"source":"cc"},"destination":"cc"},]
}, {
"name": "db2",
"servers": ["srv2", "srv2"],
"prop": [{"source":"dd"},"destination":"dd"},{"source":"ee"},"destination":"ee"},]
}
]
}
我尝试构建一个 JMESPath 表达式来 select prop
应用程序在主数组中的每个对象中,但基于 servers
元素中存在的字符串。
对select所有道具,我能做到:
*.props [*]
但是,只有当 srv1
在服务器列表中时,我才能添加条件 "select"?
您可以使用 contains
函数根据包含某些内容的数组进行过滤。
鉴于查询:
*[?contains(servers, `srv1`)].prop | [][]
这给了我们:
[
{
"source": "aa",
"destination": "bb"
},
{
"source": "cc",
"destination": "cc"
}
]
请注意,我在这里也使用了一些 flattening。
所有这些 运行 都是为了你的更正版本 JSON:
{
"srv_config":[
{
"name":"db1",
"servers":[
"srv1",
"srv2"
],
"prop":[
{
"source":"aa",
"destination":"bb"
},
{
"source":"cc",
"destination":"cc"
}
]
},
{
"name":"db2",
"servers":[
"srv2",
"srv2"
],
"prop":[
{
"source":"dd",
"destination":"dd"
},
{
"source":"ee",
"destination":"ee"
}
]
}
]
}