Skip to main content
Question

add an if condition to the json templater

  • March 20, 2024
  • 1 reply
  • 395 views

katt
Supporter
Forum|alt.badge.img+12
  • Supporter

Hello, I am creating a JSON file using several fme attributes. I want to include an if condition. If the typ =’HC’, I want to have an additional attribute (the name) added to the JSON. is it possible? The way the if condition is written here is not correct but it gives you an idea what I would like to do.

{"Device": 
{
"Type": fme:get-attribute("TYP"),
"latitude": xs:decimal(fme:get-attribute("LAT")),
"longitude": xs:decimal(fme:get-attribute("LON")),


if fme:get-attribute("TYP") eq "HC"
    then "Name": fme:get-attribute("NM")

}
}

1 reply

debbiatsafe
Safer
Forum|alt.badge.img+20

Hello @katt 

Yes, it is possible to use an if-then-else statement within the JSONTemplater as demonstrated in this Community post.

Since your if-then-else is part of an object within the “device” key, the examples in the linked Community post will not create your desired output without some additional configuration.

You can add a device sub-template for the dynamic object created with a group-by set and feed features into both the root and sub-template.

Alternatively, you can assign the dynamic object constructed to a JSONiq variable and use the return statement to assign the dynamic object as the device object’s value.

Attached is an example workspace illustrating both approaches, and I hope it helps.


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