[SalesForce] Parse JSON string in AMPscript

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

{ "User_List": "[{\"name\":\"john doe\", \"email\":\"john.doe@mail.com\"}, {\"name\":\"jane doe\", \"email\":\"jane.doe@mail.com\"}]" }

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.

Best Answer

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\":\"john.doe@mail.com\"},{\"name\":\"jane doe\",\"email\":\"jane.doe@mail.com\"}]"

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}}
Related Topic