This guide will convert JSON to another JSON using Handlebars Task. The Task will use widely used handlebars.js, via Handlebars.Net, to power 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 Handlebars Task are a different thing than everywhere else in frends.

Handlebars Task can be used to produce any text data, like CSV, XML, or JSON. In this example we create JSON. The Handlebars Task is best used in situations when you need to loop through the data set, but don't want to use C# or LINQ for that.

If you want to know more about Handlebars, please check https://handlebarsjs.com/guide/#what-is-handlebars.

In this example, the following very simple Process is created.

Example transformation

In this example following JSON is used as an input for Handlebars Task.

{
'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'
}

}

The input data is saved to a variable called BankCards to be used later.

Then the input data is used via a variable and a template used to transform the data is given. Not that the input mode of the template needs to be Expression because other vice handlebars used in this Task would get confused by handlebars used everywhere in Frends. Because Expression mode is used, a template is given as a verbatim C# string. That means @" in front of the string and " in the end. In verbatim quote needs to be escaped with another quote. All other characters are passed as-is.

Here is the same template in copy-able format.

@"{{#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}} is used to loop through each card in JSON. {{#if @first}} and {{/if}} are used to test if looping is on the card, and if is 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 curly brace { is always written to create the start of JSON object within an array. Next lines, such as ""BankerName"": ""{{../Banker.Name}}"", will write and 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 output JSON.

{{#unless @last}},{{/unless}} will check if looping is on the last card, and if not it will write a comma, to separate objects within the JSON array in output JSON. I.e. comma is not written after the last item in the array. Finally, the last characters ] } are written to the end JSON array.

The result from example transformation

As a result, we get the following JSON from the task.

[
{
"BankerName": "Patrick Bateman",
"CustomerName": "Nick",
"Credit": 2000,
"Balance": 31.24
},
{
"BankerName": "Patrick Bateman",
"CustomerName": "Emma",
"Credit": 406.05,
"Balance": 850.43
},
{
"BankerName": "Patrick Bateman",
"CustomerName": "Victor",
"Credit": 2500.05,
"Balance": 4065.12
},
{
"BankerName": "Patrick Bateman",
"CustomerName": "Emilia",
"Credit": 1500,
"Balance": 745.3
},
{
"BankerName": "Patrick Bateman",
"CustomerName": "Ralph",
"Credit": 400,
"Balance": 2040.14
}
]

Did this answer your question?