Solved

JSONTemplater, double counter in for loop

  • 23 March 2022
  • 2 replies
  • 8 views

Badge +1

Hi All,

Helped by info in this forum I constructed a loop in the JSONTemplater, to loop through a list and make a json array based on info if a certain condition is true. This works, I use a $i to reference to the index in the list. In a different counter $c I would like to keep up with how many values are entered i the array and use this value in the array as wel, but I can't get the $c counter to count up.

This is what I have so far:

[{
let $top5s := fme:get-list-attribute("_prio{}.top5")
let $themas := fme:get-list-attribute("_prio{}.thema")
let $deelthemas := fme:get-list-attribute("_prio{}.deelthema")
let $c := 0
for $top5 at $i in $top5s
where xs:string($top5s[$i]) eq "Ja"
let $c := $c + 1
return {"titels": "Projectambitie " || $c ||": " || $deelthemas[$i]  ,
        "teksten": "Op basis van de locatie vraagt " || $deelthemas[$i] || " aandacht in je project."
    }
}]

It works, only $c stays at value 1...

Anyone got any tips, I probably am overlooking something

 

Regards,

Steven

icon

Best answer by debbiatsafe 24 March 2022, 01:13

View original

2 replies

Userlevel 3
Badge +17

Hi @stevens​ 

I'm far from an XQuery expert but this suggested using count. I tested it and it seems to work to produce the output you want.

[{
    let $top5s := fme:get-list-attribute("_prio{}.top5")
    let $themas := fme:get-list-attribute("_prio{}.thema")
    let $deelthemas := fme:get-list-attribute("_prio{}.deelthema")
 
    for $top5 at $i in $top5s
    where xs:string($top5s[$i]) eq "Yes"
    count $c
    return {"titels": "Projectambitie " || $c || ": " || $deelthemas[$i]  ,
            "teksten": "Op basis van de locatie vraagt " || $deelthemas[$i] || " aandacht in je project."
        }
}]

Note there is a short explanation here on why $c := $c +1 doesn't work which I found informative.

Badge +1

Thanks, Debbi! That did the trick! The background info was very helpful for understanding!

Reply