Question On Use of Built-In JSON Parser and Generator

Helpful workarounds, but since this invalid JSON is created by a webhook template, it would be nice to see some tweaks on the templating logic for in the webhook system to allow for creating an always valid output.

The template in question came from this

Not adding the comma and adding the comma in the template will both times result in an invalid output. When a list is rendered (indicated by [ ... ]) shouldn't the template expansion know how to render the contents correctly?

How would one create a template that creates a valid JSON from a valid JSON other than that?

Also, shouldn't the template expansion render a 0 or "" result for non-present keys?
The added 0 only works when the value is a floatint point value, it would change the value for an integer and render invalid results for string values. Since the raw webhook response may well be rendering different types when the key is present, a generic approach would be needed.
An alternative would be to make the key creation in the expanded result depending on the presens of the value - but I don't see how to code that in the template either.

Shouldn't one of these work?

{ "daily": [{{#daily}} { "dt":{{dt}}, "rain":{{#if rain}} {{rain}} {{else}} 0.0 {{/if}} } {{/daily}}] }
  or
{ "daily": [{{#daily}} { "dt":{{dt}}, "rain":{{#with rain}} {{rain}} {{else}} 0.0 {{/with}} } {{/daily}}] }
 or
{ "daily": [{{#daily}} { "dt":{{dt}}, "rain":{{rain}} {{#unless rain}} 0.0 {{/unless}} } {{/daily}}] }

but neither render a result - apparently the if, with and unless helpers are not declared although they should be built-in

1 Like