I have a dynamically changing JSON array, of unknown size and I need to reference the array length using JMESPath query.
For example
{
"locations": [
{"name": "Seattle", "state": "WA"},
{"name": "New York", "state": "NY"},
{"name": "Bellevue", "state": "WA"},
{"name": "Olympia", "state": "WA"}
]
}
If I use a query like length(locations)
, I get the desired result, 4
.
And if I use locations[0], I get desired result:
{
"name": "Seattle",
"state": "WA"
}
But, lets say I want to combine both the queries like locations[length(locations)]
which should translate to locations[4]
, this does not work at all. I tried many combinations to get the array length but this is not working.
Can anyone help me here?
If you want to target the last element, you can also use negative indexes:
locations[-1]
Would yield:
{
"name": "Olympia",
"state": "WA"
}