Got an odd request but trying to figure out if there is a way to emulate the behaviour of the JSON Template in a Workflow node within a workflow. By that I mean, I would like to take a JSON object that already includes handlebar variable references (ie. “{{data.path.something.interesting}}” ) and expand this with variable substitution. I believe this is triggered when adding a Workflow or Device Command as the output of a workflow, but it appears this expansion is only processed for single level JSON objects. If I have a nested JSON object that I am including in the JSON template with {{jsonEncode data.path.something.interesting}}
then the variable expansion is ignored.
I’m not sure I exactly follow what you are trying to do. In your final example, where you have {{jsonEncode data.path.something.interesting}}
, could you give an example for the value at data.path.something.interesting
, and what it does right now vs. what you wish it would do?
Ok I think I have answered my own question detailing this out for you. My variables are embedded within text strings within the JSON payload and hence being treated as literal strings
Lets say the {{jsonEncode data.nestedPayload}}
looks something like this:
[{
"string": "This is a string with an embedded {{data.variable}} ",
"firstName": "{{data.object.first_name}}",
"lastName": "{{data.object.last_name}}"
},
{
. . . .
}
]
I will have to look at the String node with Replace Occurrence I guess to manage any variables in strings payloads. Sorry for the answered question.
Ah, ok. We don’t have anything that will deeply iterate through an object and do template replacement, however, we do have a better option than using the string node. There is an advanced handlebars helper named template
, which will run a string through handlebars and do template replacement. In your case, for example, you might be able to get away with doing {{template (jsonEncode data.nestedPayload)}}
- jsonEncode
would convert your object to a string, complete with template variables, and template
will run our standard template engine on that string. You would run into issues if the templating ends up resulting in an invalid json string, though - i.e., things that need to be escaped not getting escaped.
A more correct way would be to use the template
helper directly on each individual field first, replacing the string with templates with the resulting string (probably using the Mutate node), and then do jsonEncode
, but you would need to know the object keys to replace - so that isn’t really a generic “any nested object” solution.
Couldn’t find this template
helper in the documentation. Very useful tool! I have an event handling engine that stores “rules” to be checked in a data table, and this allowed me to include templateable message strings for the generated events.