This guide will demonstrate how to convert JSON to another JSON using the Handlebars Task. The Task will use handlebars.js, a widely used library, via Handlebars.Net to perform any JSON-to-text transformations. Handlebars is such a popular concept that it inspired Frends handlebars. Hopefully, it won't cause too much confusion that handlebars within the Handlebars Task are different from handlebars used elsewhere in Frends.
Handlebars Task can be used to produce any text data, such as CSV, XML, or JSON. In this example, we will create JSON. The Handlebars Task is best used in situations when you need to loop through the data set but do not want to use C# or LINQ for that.
If you want to learn more about Handlebars, please check out https://handlebarsjs.com/guide/#what-is-handlebars.
Example transformation
Save following JSON as an input data to a variable called "bankCards".
{'Cards': [{
'id': 3201,
'Name': 'Nick',
'Balance': {
'Debit': 31.24,
'Credit': 2000.00
}
},
{
'id': 6676,
'Name': 'Emma',
'Balance': {
'Debit': 850.43,
'Credit': 406.05
}
},
{
'Name': 'Victor',
'Balance': {
'Debit': 4065.12,
'Credit': 2500.05
}
},
{
'id': 9053,
'Name': 'Emilia',
'Balance': {
'Debit': 745.30,
'Credit': 1500.00
}
},
{
'id': 6492,
'Name': 'Ralph',
'Balance': {
'Debit': 2040.14,
'Credit': 400.00
}
}
],
'Banker': {
'Name': 'Patrick Bateman'
}
}
Then, the input data is passed through a variable, and a model is used to transform the data. Note that the input mode of the template needs to be "Expression" because handlebars used in this Task would get confused by handlebars used everywhere else in Frends. Because Expression mode is used, a template is given as a verbatim C# string. That means @" is added in front of the string and " is added at the end. In a verbatim quote, another quote needs to be escaped with another quote. All other characters are passed as-is.
@"{{#each Cards}}{{#if @first}}{""BankCards"":[{{/if}}{""BankerName"":""{{../Banker.Name}}"",""CustomerName"":""{{{Name}}} "",""Credit"":{{Balance.Credit}},""Balance"":{{Balance.Debit}} }{{#unless @last}},{{/unless}}{{/each}}]}"
Used template explained
{{#each Cards}} and {{/each}} are used to loop through each card in the JSON. {{#if @first}} and {{/if}} are used to test if the looping is on the first card, and if so, the beginning of the output JSON structure is written. Here, we start an array { "BankCards": [. Of course, it would be possible to write this before starting looping, but this was a convenient way to introduce if-block.
Next, a curly brace { is always written to create the start of a JSON object within an array. The next lines, such as ""BankerName"": ""{{../Banker.Name}}"", will write an attribute to the output JSON. Two dots in {{../Banker.Name}} will go one level upwards in the hierarchy, then it will go to Banker and select Name from it. In the next line, "triple-stash," three curly braces, are used around Name to prevent HTML-escaping. This ensures that non-ASCII characters are passed as-is to the output JSON.
{{#unless @last}},{{/unless}} will check if the looping is on the last card, and if not, it will write a comma to separate objects within the JSON array in the output JSON. In other words, a comma is not written after the last item in the array. Finally, the last characters ] } are written to the end of the JSON array.
The result from example transformation
As a result, we get the following JSON from the Task.