[英]Get value of parent element by child jq
Code:代码:
{
"endpointAgents":[
{
"agentId": "MyId",
"agentName": "MYNAME",
"location": {
"locationName": "location"
},
"clients": [
{
"userProfile": {
"userName": "Name"
},
"browserExtensions": [
{
"active": false
}
]
},
],
"totalMemory": "16222 MB",
"agentType": "enterprise"
}
I need to return the agentId value with the value userName .我需要返回值为userName的agentId值。 I know how to do it with JSONPath我知道如何使用 JSONPath
($.endpointAgents[?(@.clients.userName=~ 'a')].agentId) ($.endpointAgents[?(@.clients.userName=~ 'a')].agentId)
, but don't know how with jq. ,但不知道jq如何。
Assuming your input JSON is假设您的输入 JSON 是
{
"endpointAgents": [
{
"agentId": "MyId",
"agentName": "MYNAME",
"location": {
"locationName": "location"
},
"clients": [
{
"userProfile": {
"userName": "Name"
},
"browserExtensions": [
{
"active": false
}
]
}
],
"totalMemory": "16222 MB",
"agentType": "enterprise"
}
]
}
To get the agentId
values from all items of the endpointAgents
array where in the same object at least one object in the clients
array has a userProfile.userName
string value that contains a given substring, I'd go with To get the agentId
values from all items of the endpointAgents
array where in the same object at least one object in the clients
array has a userProfile.userName
string value that contains a given substring, I'd go with
jq -r '
.endpointAgents[]
| select(.clients | map(.userProfile.userName | contains("a")) | any)
| .agentId
'
MyId
In order import the query string from outside jq, use the --arg
parameter要从 jq 外部导入查询字符串,请使用--arg
参数
jq -r --arg query "a" ' … contains($query) … '
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.