Find your content:

Search form

You are here

Can AMPScript parse JSON?

 
Share

Using AMPScript to parse an XML feed. Client is switching to JSON. Is there something similar to BuildrowsetfromXML() for JSON in AMPScript? I am confined to using the members login area and not an external API application.


Attribution to: xtianjs

Possible Suggestion/Solution #1

This worked for me. Passing Recipient_List as a JSON string attribute to a email template.

"[{\"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}}

Attribution to: Tim Graf

Possible Suggestion/Solution #2

Here's a simple Guide Template Language example:

%%[

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

]%%
{{.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: john.doe@mail.com 

Name: jane doe 
Email: jane.doe@mail.com 

NOTE: This doesn't appear to work in Content Builder.


Attribution to: Adam Spriggs

Possible Suggestion/Solution #3

I did this using SSJS and eval(). I could not find a way to parse JSON in AMPscript. Also if I recall correctly JSON.parse() is not implemented in ExactTarget.

There are security concerns with eval() so you should only use it with a trusted source.

Here is my example, where I first use HttpGET() in AMPscript to retrieve JSON from my trusted source.

%%[ set @JSON = HttpGET("http://some-url-that-returns-json") ]%% 

<script runat="server">
    Platform.Load("Core", "1")
    // get JSON from ampscript
    var jsonObj = Variable.GetValue("@JSON");
    var evaluatedJSON = eval("(" + jsonObj + ")");
</script>

Attribution to: Jon Sakas

Possible Suggestion/Solution #4

I use the SSJS function Platform.Function.ParseJSON.

<script runat="server">
     var str = '{ "prop1": "propVal" }';
     var obj = Platform.Function.ParseJSON(str);
     var val = obj.prop1;
</script>

Attribution to: SebOB
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/34945

My Block Status

My Block Content