Skip to main content
Solved

JSONTemplater, double counter in for loop


Forum|alt.badge.img+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

Best answer by debbiatsafe

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.

View original
Did this help you find an answer to your question?

2 replies

debbiatsafe
Safer
Forum|alt.badge.img+20
  • Safer
  • Best Answer
  • March 24, 2022

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.


Forum|alt.badge.img+1
  • Author
  • March 24, 2022

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


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings