How to implement a for loop in Jsonata

2.1k Views Asked by At

I am looking to convert xquery to jsonata ,

let $h := $at//datahistory
for $x in 0 to (count($h))
let p:= (if (not($h[$x]/daytoday =(‘one’, ‘two’) )) then ‘ZERO’ else $h[$x+1]/Status 
return if(index-of($p) > 0 then 0 else 1)

Any kind of assistance and help is much appreciated.

Thanks in advance.

1

There are 1 best solutions below

4
On

Your XQuery is a for expression that can be easily rewritten as a simple XPath path expression /bookstore/book[price > 30]/title which would translate to a JSONata path bookstore.book[price > 30].title. On the other hand it is unlikely that the JSON data has such a structure with bookstore and book, I guess an object with an array property like

{
  "bookstore" : [
    {
      "title" : "title 1",
      "price" : 20
    },
    {
      "title" : "title 2",
      "price" : 35
    },
    {
      "title" : "title 3",
      "price" : 99
    }
  ]
}

which leads to bookstore[price > 30].title (https://try.jsonata.org/0vfDXqv4m) or a simple array with the book objects

 [
    {
      "price": 20,
      "title": "title 1"
    },
    {
      "price": 35,
      "title": "title 2"
    },
    {
      "price": 99,
      "title": "title 3"
    }
]

which leads to $[price > 30].title (https://try.jsonata.org/fryxG-QLj) is more likely.