Skip to main content

Good day,

I am using a JSON-Templater.

My Root Template looks like this:

{"GisData":
    {"Lake" : :fme:process-features("LAKE")],
        "River" : efme:process-features("RIVER")], 
        "Park : fme:process-features("PARK")]
        }   
}

and in the Sub Template I have one port for the Lake, one for the River and one for the Park.

In some cases I don’t have lakes for the JSON I want to create. So the JSON will look like this: "Lake": l], This is not useful and will create issues later on. How can I specify that I only want to add them to the JSON if they have a value ?

Hello @katt 

You could use the JSONFormatter with the “Remove Empty Objects and Arrays” parameter enabled to remove the JSON objects with empty arrays.


Hi @debbiatsafe thank you. there is no way to add an if clause in the JSON templater directly? I wanna use as few transformers as possible.

Btw I tried your solution: it removed the empty one, but for other attributes it changed the attribute decimal value of the json templater result e.g. from "fromNumber": 0.0009, to "fromNumber": 0.0009000000000000001,
why ? and what is the setting to keep the original value as I don’t want the formatter to modify any value?


Hi @katt 

It should be possible to use a conditional in the JSONTemplater. The specific configuration in the JSONTemplater would depend on the structure of your data.

I have attached an example where the JSONTemplater tests if the park, river, or lake attributes exist on the root feature via the fme:has-attribute function to determine whether to add the respective JSON objects or not.

0.0009 becoming 0.0009000000000000001 is caused by floating point precision with certain data types.  I don’t believe there is a way of avoiding this with the JSONFormatter currently, as it is an issue with the library used in the transformer.


Hi @debbiatsafe , thank you for your help. In my case all the attributes of the subtemplate ports are the same (id, lat,long) so I can’t use the get-attribute to determine if one of the list is empty. Is there something similar to this if statement as this one doesn’t work:

if 
    (fme:process-features("LAKE") != null)
then

    ("Lakes" : (fme:process-features("LAKE")])


Hi @katt 

The JSONTemplater’s root port is not aware whether any features will arrive at the sub-template ports or not. You can force an awareness by using specific attributes and testing for the presence of these attributes on the root template.

You can do this by adopting the approach used in my previous response and manually create an attribute using an AttributeCreator that determines whether there are features for the specific sub-templates. Then use the Aggregator to merge all attributes into one feature to send to the root port of the JSONTemplater.


Reply