I’m new in JMESPath, so I’m not so familiar with a complex query. I have tried quite a lot of queries and they are far from giving me the final results that I wanted.
Target:
Get the first instanceId of the deallocated Azure VMSS.
Detail
- Deallocated Azure VMSS can be filtered by its status code ==
PowerState/deallocated
- First instanceId is determined by sorting the time of
ProvisioningState/succeeded
.
Tester: https://jmespath.org/
Sample JSON: (stripped output of az vmss list-instances
command)
[
{
"instanceId": "5",
"instanceView": {
"statuses": [
{
"code": "ProvisioningState/succeeded",
"time": "2023-10-24T14:18:08.8438814+00:00"
},
{
"code": "PowerState/deallocated"
}
]
}
},
{
"instanceId": "13",
"instanceView": {
"statuses": [
{
"code": "ProvisioningState/succeeded",
"time": "2023-10-24T15:53:59.6296842+00:00"
},
{
"code": "PowerState/running"
}
]
}
}
]
Expected Output:
5
(because it is the first provisioned instance ID and the state is deallocated)
Failed Attempts:
-
sort_by([].{InstanceID: instanceId, Time: instanceView.statuses[0].time}, &Time)[0].InstanceID
Getting the instanceID of the first provisioned instance, but not sure how filter the deallocated from here. -
[*].instanceView.statuses[?code=='PowerState/deallocated']
: Project only deallocated statuses, not sure how to project the instanceId from here. -
[].instanceView.statuses[?code=='PowerState/deallocated'][].{instanceId: instanceId ,status:code}
attempting to project the instance ID but got null.
2
Answers
This answer is based on my own attempt, and it seems to work. It recreate the sorting object by stripped out everything that isn't useful prior to the sorting, and keep the sorting key a simple variable.
&Time
and also simpleState
filter.Have a look at the other answer https://stackoverflow.com/a/77355415/764592 for a different approach.
Preamble: for the sake of demonstrating the correct behaviour of the code provided, I included a third element in your array, in order to have two element with the
PowerState/deallocated
and observe the correct behaviour of the sorting.So here is the sample JSON I am using, which include an instance of identifier
3
:I think the major concept missing in your actual trials is the fact that you have to stop an existing projection in order to get back an array if you want to pick the first element of an array.
This is further explained in the chapter called "pipe expressions" of the tutorial:
This is most likely what is causing you to have
null
returned in your third attempt.Now, for your specific use case, I would split it into three steps:
PowerState/deallocated
.This can be achieved by crafting the filter projection in the top level array:
Which gives:
So the field to sort on would be
And the whole sort function, then, be
Which gives:
sort_by
function, you will have to stop the projection once again, before getting theinstanceId
:So, your final query ends up being
And your result, as expected, would be