Question

JSON Templater - how to export attributes with value?


Badge +1

Hello everybody,

please, Im trying to automate creation of JSON and I need to get exported JSONs only with attributes, which are exposed and have values (not null etc.). Please, does anybody know how to solve this problem via JSON Templater?

Thank You so much!

Lubo


5 replies

Badge +22

If I understand correctly,  you know the attributes you want to include in the json, but you only want to insert them if there is a value.

 

 

Using the JSONTemplater, you can use an xquery conditional statement
{
 if (fme:has-attribute("A"))
 then {"A": fme:get-attribute("A")}
 else ()

Userlevel 4

You can also try using the JSONFormatter after the JSONTemplater:

If you have a lot of attributes in your JSONTemplater it may be a bit less work than creating an if-statement for each of them, although I suspect it's somewhat faster in execution.

Badge +1

Hello @jdh , @david_r,

thank both of you! Probably, I should describe deeply my issue - As source data, I have many feature types with different spectrum of attributes. And I wanna convert data of them to JSON file as easy as possible. So I dont wanna define special template in JSON templater for every feature type, but I need one general definition - you can imagine that as dynamic mode of attributes writing on writer.

Is there any way how to reach that? Your supposed way could be used, but it means for me to prepare template in JSON templater, where I should write all names of attributes.

Thank You so much!

Lubo

 

Userlevel 2
Badge +17

If I understand your requirement correctly, this workspace example would be a possible way.

dynamic-jsontemplater.fmwt (FME 2019.0.0.1)

Badge +3

If I understand your requirement correctly, this workspace example would be a possible way.

dynamic-jsontemplater.fmwt (FME 2019.0.0.1)

@Takashi Iijima​ excellent solution 👍

Reply