Question

How to insert an empty object in JSONTemplater?

  • 12 June 2024
  • 2 replies
  • 29 views

Badge +1

I need to create a JSON attribute with a key that only has an empty object as its value as you can see at key1.

{
"key1": {},
"key2": {
...
}
"key3": [
...
]
}

Problem is that the JSONTemplater turns empty objects into null. An empty object is valid JSON and is very different from the value null so this behavior is completely incorrect.

How can I set an empty object as a value inside a JSON attribute?

I tried using the JSONUpdater but it works in the same way. If you set value type to JSON/XQuery it replaces the object with null. If you set the type to plain text, it only creates a string “{}” containing two curly braces.


2 replies

Userlevel 4
Badge +19

Hello @dsign-world 

If you add an empty array inside the object, you should be able to create an empty JSON object using the JSONTemplater. For example, the following in the JSONTemplater:

{
"key1": {[]},
"key2": {"key": 2}
}

will create

{ "key1" : {  }, "key2" : { "key" : 2 } }

I hope this information helps.

Userlevel 5
Badge +32

Another options is to use an attribute value of “{}” (without quotes). In the JSON template you can refer to its value using the fme:get-json-attribute("key1") function. This will treat it like json. 
 

 

{
"key1": {
}
}

This is the output I get form the methos above. 
Indeed the same is true to using the JSONUpdator. You can use the fme:get-json-attribute("<some_json_attribute>") in the Value field to prevent FME from escaping the characters. FME will treat it a bit differntly. 

Reply