0

We are currently passing a string attribute via the REST API that contains JSON.

{ "User_List": "[{\"name\":\"john doe\", \"email\":\"[email protected]\"}, {\"name\":\"jane doe\", \"email\":\"[email protected]\"}]" }

We then use server side JavaScript to parse the attribute string value into JSON.

<script runat=server>

  Platform.Load("Core","1");

  var userList    = Platform.Recipient.GetAttributeValue("User_List");
  var userListObj = Platform.Function.ParseJSON(userList);

</script> 

We then later use that JSON to build a dynamic list from the JSON data.

<p>
  <script runat=server>
    Write("<ul>");

    for (var i = 0; i < userListObj.length; i++) {
      Write("<li>" + userListObj[i].name + ", " + userListObj[i].email + "</li>");
    }

    Write("</ul>");
  </script> 
</p>

My question is, is there a way to do this via AMPscript? I couldn't find anything about parsing a string into JSON via AMPscript.

0

2 Answers 2

1

So I may have not asked the correct question for what I was trying to accomplish. This worked for me. Passing Recipient_List as a JSON string attribute.

"[{\"name\":\"john doe\",\"email\":\"[email protected]\"},{\"name\":\"jane doe\",\"email\":\"[email protected]\"}]"

With the JSON string input shown above.

%%[
   var @json
   set @json = AttributeValue("Recipient_List")
]%%

{{.dataobject JsonVar type=variable source=@json maxrows=20}}
   {{.data}}
        {"target":"@Json"}
   {{/data}}
{{/dataobject}}
{{#each JsonVar}}
   <p>{{name}}, {{email}}</p>
{{/each}}
0

There are no JSON parsing functions in AMPScript. Most of the parsing I've seen is for XML, since AMPScript has several XML parsing functions.

Talk to anyone at SFMC they'll steer you away from using SSJS in the platform. It's slow and very resource intensive. It's incredibly helpful in landing pages, but once you start mentioning it in email, they'll advise against it.

You can use AMPScript for most everything -- aside from arrays and things like parsing JSON.

There's more discussion here: Include JSON Feed in an Email

Update

Here's a simple GTL example as @Tim Reynolds suggested.

%%[

var @Json
set @Json = '[{"name": "john doe","email": "[email protected]"},{"name": "jane doe","email": "[email protected]"}]'

]%%
{{.dataobject JsonVar type=variable source=@Json maxrows=20}}
     {{.data}}
          {"target":"@Json"}
     {{/data}}
{{/dataobject}}
{{#each JsonVar}}
  <br><br>Name: {{name}}
  <br>Email: {{email}}
{{/each}}

Output

Name: john doe 
Email: [email protected] 

Name: jane doe 
Email: [email protected] 
4
  • This is something I am working on right now, and I hope you are wrong because I am counting on using AMPScript to parse JSON. This page directly mentions AMPScript but is under GTScript heading... help.exacttarget.com/sv/documentation/guide_template_language/… Commented Feb 4, 2016 at 16:19
  • 1
    You may be correct. Mixing AMPScript and GTL may be the best route to take. I've generally poo-poo'd GTL since it doesn't work on Landing Pages/Microsites. Commented Feb 4, 2016 at 17:13
  • Copied this code verbatim and received the following error. Any ideas? The data source referenced by a Guide Each tag was not found. DataSourceIDType: Unspecified DataSourceID: JsonVar Guide Tag: {{#each JsonVar}} Tag Index: 7761
    – Daniel
    Commented Nov 29, 2018 at 19:17
  • It appears the issues is only when using the code within a content block on a Content Builder template. Works fine in HTML paste.
    – Daniel
    Commented Nov 29, 2018 at 23:37

You must log in to answer this question.

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