Filter azure cli output based on nested array values with jmespath
I'm trying to filter the output from the following Azure CLI command based on the artifacts[].alias
value.
Command
az pipelines release definition list --artifact-type build
Output (simplified)
[
{
"artifacts": [
{
"alias": "alias_with_suffix",
"definitionReference": {
"IsMultiDefinitionType": {
"id": "False",
"name": "False"
}
},
"type": "Build"
}
],
"createdOn": "2021-06-16T15:15:07.620000+00:00",
"id": 88,
"isDeleted": false,
"modifiedOn": "2021-09-15T10:39:14.257000+00:00",
"revision": 5
},
{
"artifacts": [
{
"alias": "alias",
"definitionReference": {
"IsMultiDefinitionType": {
"id": "False",
"name": "False"
}
},
"type": "Build"
}
],
"createdOn": "2021-06-16T15:15:07.620000+00:00",
"id": 88,
"isDeleted": false,
"modifiedOn": "2021-09-15T10:39:14.257000+00:00",
"revision": 5
},
{
"artifacts": null,
"createdOn": "2021-06-16T15:15:07.620000+00:00",
"id": 88,
"isDeleted": false,
"modifiedOn": "2021-09-15T10:39:14.257000+00:00",
"revision": 5
}
]
Expected Output
[
{
"artifacts": [
{
"alias": "alias_with_suffix",
"definitionReference": {
"IsMultiDefinitionType": {
"id": "False",
"name": "False"
}
},
"type": "Build"
}
],
"createdOn": "2021-06-16T15:15:07.620000+00:00",
"id": 88,
"isDeleted": false,
"modifiedOn": "2021-09-15T10:39:14.257000+00:00",
"revision": 5
}
]
So far i have tried the following:
-
az pipelines release definition list --artifact-type build --query "[artifacts[?ends_with(alias, '_suffix')]]"
-
az pipelines release definition list --artifact-type build --query "[].[artifacts[?ends_with(alias, '_suffix')]]"
-
az pipelines release definition list --artifact-type build --query "[].artifacts[?ends_with(alias, '_suffix')]"
I've also tried to use something like [*].....
or pipe results between commands like [] | [].....
(with or without the *
) but I always get something like [null]
or some output with a list of null
mixed with valid values.
For this, you will have to have a "nested" filter expression, because you are interested in the "parent" object based on the fact that a condition in the array of key artifacts
is true.
So, given the query:
[?artifacts[?ends_with(alias, '_suffix')]]
You end up, with the JSON you provided as input, with this resulting JSON:
[
{
"artifacts": [
{
"alias": "alias_with_suffix",
"definitionReference": {
"IsMultiDefinitionType": {
"id": "False",
"name": "False"
}
},
"type": "Build"
}
],
"createdOn": "2021-06-16T15:15:07.620000+00:00",
"id": 88,
"isDeleted": false,
"modifiedOn": "2021-09-15T10:39:14.257000+00:00",
"revision": 5
}
]