0

I am trying to parse a JSON which I get after doing a post call in the cloud page, The returned JSON is nested and I can't access it, I have tried the GLT but it is not working. This is my JSON :

  {
  "message": {
    "links": {
        "self": {
            "href": "/v1/emails/1734
        }
    },
    "views": [
        {
            "contentType": "vnd.exacttarget.message.email.htmlBody",
            "content": “Hello”
        }
      ]
    }
 }

I am trying to extract parse the content and then be able to display it.

I have tried the following code :

{{.datasource JSONVar type=variable maxRows = 20}}
   {{.data}}
        {"target" : "@accesstoken"}
    {{/data}}
      {{.datasource message type=nested maxRows = 20}}
          {{.data}}
                    { "target" : "JsonVar.message" }
          {{/data}}
                <br>JsonCustomer.links: {{links}}                
             {{.datasource Jsonviews type=nested maxRows = 20}}
               {{.data}}
                    { "target" : "message.views" }
               {{/data}}
                <br>Jsonviews.contentType: {{contentType}}
                <br>Jsonviews.content: {{content}}

   %%[
  var @content

  ]%%

                <br>Hello %%=v(@content)=%% 
                <hr>

              {{/datasource}}
        {{/datasource}}
  {{/datasource}}

@accesstoken is the JSON. Any help is appreciable, thank you

1
  • Pretty sure GTL doesn't work in CloudPages. You'll have to use SSJS to parse and iterate through the response. Commented Jan 17, 2020 at 22:06

2 Answers 2

6

Here's a parsing example using SSJS.

If this were the value of the json attribute:

[
   {
      "sku":"123",
      "name":"Square",
      "url":"https://spmvs.com?sku=123"
   },
   {
      "sku":"456",
      "name":"Circle",
      "url":"https://spmvs.com/?sku=456"
   },
   {
      "sku":"789",
      "name":"Triangle",
      "url":"https://spmvs.com/?sku=789"
   }
]

Here's how I'd parse it with SSJS (and break out and output it with AMPscript):

<script runat="server">

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

var json = Attribute.GetValue('json');
var jsonObj = Platform.Function.ParseJSON(json);

if (jsonObj.length > 0) {

  for (var i = 0; i < jsonObj.length; i++ ) {

      var item = jsonObj[i];

      Platform.Variable.SetValue("@sku",item['sku']);
      Platform.Variable.SetValue("@name",item['name']);
      Platform.Variable.SetValue("@url",item['url']);

      </script>

      <br><a href="%%=redirectto(@url)=%%">%%=v(@sku)=%%</a> - <a href="%%=redirectto(@url)=%%">%%=v(@name)=%%</a>

      <script runat="server">

  }

} else {

  Write("no products found")

}
</script>
0

I was able to parse through and get the value of content and everything as an array, This is the method I used

 <script runat="server">
 Platform.Load("Core", "1");
 var arr = [];
 var jsonString1 = Variable.GetValue("@response2");
 arr.push(Platform.Function.ParseJSON(jsonString1).message.views[0].content);
// Write("<br>" +arr);
 Variable.SetValue("@Array",Stringify(arr));

  </script>
%%%=v(@Array)=%%

You must log in to answer this question.

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