skip to Main Content

Let’s say I have the following JSON

{
 "Fruits" : ["apple", "mango", "banana"];
 "Index" : 2
}

How do i navigate the array "Fruits" such that i could use index as the index number I.e. If i write Fruits[Index], it should give me "banana".

I tried the code mentioned above and I got it always as "no match".

2

Answers


  1. When you type an expression inside the filter operator (inside square brackets), you’re entering the local context of the entries that you’re filtering, and all your expressions become local to each array entry instead of the object root.

    To break away from that local context you can use the special $$ variable to access the Index property on the root object.

    Here’s the solution:

    Fruits[$$.Index]
    

    Check it out on the Stedi Playground: https://stedi.link/5gbpkfc

    Login or Signup to reply.
  2. The % operator allows you to access the parent context. You could then get the Index from that.

    Fruits[%.Index]
    

    You may also store the index as a variable to later use to index the array.

    ($i:=Index; Fruits[$i])
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search