[SalesForce] How does Guide Template Language parse a straight JSON array

I'm learning Guide, and I've been able to parse an array of objects…

AmpScript:

set @json = '[{"voucher":"123456789"}, {"voucher":"234567890"}]'

Guide:

    {{.dataobject JsonVar type=variable source=@json maxrows=20}}
       {{.data}}
            {"target":"@json"}
       {{/data}}
    {{/dataobject}}
    {{#each JsonVar}}
       <p>{{voucher}}</p>
    {{/each}}

Returns:

123456789
234567890

But what if I want to return the same output, but instead from a simpler array?

set @json = '["123456789", "234567890"]'

I've tried everything I can find here, and in the docs, with no luck so far. Any help would be greatly appreciated!

Best Answer

Short answer is that it should, but it doesn't.

I am going off assumptions as I have no definitive proof, but even attempting to use the example in the official docs does not work.

<ul class="people_list"> 
{{#each people}}
  <li>{{.this}}</li> 
{{/each}}
</ul>

using this data:

{
  people: [
    "Yehuda Katz",
    "Alan Johnson",
    "Charles Jolley"
  ]
}

So if you use {{.this}} like it shows, it doesn't recognize it as GTL and instead just outputs {{.this}} as if it were a string.

I then also tried the other options for handlebars that I have seen, including {{this}} (without period) which seems to be the documented solution for Handlebars. But nothing worked in GTL.

If you look, you will notice that the sample in the Handlebars docs is nearly identical to the sample in GTL docs. (minus the period in GTL) I get the feeling GTL was not quite finished and although they intended to include this capability, it was never actually implemented.

Your best bet to get around this is to change the string array into an array of objects or utilize SSJS instead (my recommendation is SSJS).

Example to turn String array to Object Array:

SET @strArr = '[1,2,3]'
SET @openObj = '{ value: '
set @closeObj = '},'
set @strCSV = ReplaceList(@strArr,'','[',']')
set @strRS = BuildRowsetFromString(@strCSV)
set objArr = '['

for @i=1 to Rowcount(@strRS) do
  set @row = Row(@strRS,@i)
  set @val = Field(@row,1)
  set @obj = CONCAT(@openObj,@val,@closeObj)
  set @objArr = CONCAT(@objArr,@obj)
next @i

set @objArr = CONCAT(Substring(@objArr, 1, Subtract(Length(@objArr), 1),']')

This would then have @objArr equal to:

[{value: 1},{value: 2},{value: 3}]

So you would be able to then correctly iterate it out.

or you could just remove AMPScript and GTL completely and just use SSJS:

   var strArr = [1,2,3]
    for(i=0;i<strArr.length;i++) {
       Platform.Response.Write('<p>' + strArr[i] + '</p>')
    }

Which outputs:

1
2
3