6

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!

6
  • We have several questions and answers filed under the gtl tag Commented May 3, 2016 at 20:32
  • Thanks @AdamSpriggs! I'm specifically looking for an example of how to parse just a simple array of strings, which I haven't seen yet. I've been able to parse more complex arrays and objects, but this one's eluding me. Commented May 4, 2016 at 16:03
  • I think if there's no complex data within the array you can use BuildRowSetFromString with a comma as delimiter. Commented Jul 23, 2020 at 3:13
  • looking for an answer to this. any clue? @RonCoffman Commented Jul 29, 2020 at 9:57
  • 1
    @Gortonington Yes, thanks for the detailed analysis and answer. Yes, this is the best so far. Thanks Commented Aug 6, 2020 at 10:23

1 Answer 1

4
+50

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .