How can I delete the first element from an array while assuring the result is still an array in jsonata?

368 Views Asked by At

I would like to create a jsonata expression that deletes the first element of an array. There is a very simple expression that does that :

array1#$pos[$pos!=0]

but when applying this expression to an array having 2 elements, then this expression is not returning an array containing only the second element, but it is returning the 2nd element as you can see in the following jsonata test

https://try.jsonata.org/gl0l_wnGe

enter image description here

So this is not what I want. I am looking for a jsonata expression that for "new_array1" doesn't return "two" but returns ["two"].

This is a bit simplified version of the actual problem I am having. The actual problem is that I want to delete an element with a specific index in the array and I am having the same problem when the array has 2 elements.

1

There are 1 best solutions below

0
On BEST ANSWER

It's returning "two" instead of ["two"] because of the sequence flattening rules https://docs.jsonata.org/processing#sequences. Adding an empty [] to the end of the path expression will turn the sequence into an array. Try this:

{
    "new_array1": array1#$pos[$pos!=0][],
    "new_array2": array2#$pos[$pos!=0][]
}

https://try.jsonata.org/BaO2T7_9i