Here is JSON:
{
"a": [
"foo",
"bar",
"test"
]
}
The idea is to replace bar by bar_new, if bar is present in the array.
The desired output will be:
[
"foo",
"bar_new",
"test"
]
So, if we don't have bar in the input JSON, the output will be:
[
"foo",
"test"
]
I've tried to put it together by map() and join() functions, but join() function returns a string instead of an array.
I need exactly this array in the output.
Here is a solution, the only caveat is that it does not replace the element
bar, it removes it out of the array and appends an elementbar_newif there was an elementbarin the first place.So, effectively the element
bar_newis not guaranteed to be at the same index asbarwas.In JMESPath you cannot do an
if ... else ..., but you can apply the same principle as you will sometimes find in shell scripts where we can doSo, in your case you can start by saying:
barin the array, return me an altered version of the array:barbar_newbarin the array (effectively, an else), return me the array inaFinding an element in an array is quite simple, given that this is the role of the function
contains().Giving you a query like:
Knowing how to append an element in an array is a tricky one, there is no easy concatenation between arrays in JMESPath, this said, you can create an array of arrays and flatten it.
For example:
Would result in
Combining all of this, the query
Would yield, with your example JSON,