Question

add an if condition to the json templater

  • 21 March 2024
  • 1 reply
  • 24 views

Badge +8

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

Userlevel 3
Badge +18

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.

Reply