0

I am calling out the the MC REST API inside my SSJS (as its the only way to retrieve the data I need). I can't however figure out how to access properties from the response body.

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

try {

  var url = 'https://serverurl.auth.marketingcloudapis.com/v2/token';
  var contentType = 'application/json';
  var  payload = '{"clientId":"123",';
  payload += '"client_secret":"456"}';

  var accessTokenResult = HTTP.Post(url, contentType, payload);
  var statusCode = result["StatusCode"];
  var response = accessTokenResult["Response"][0];
  var accessToken = Platform.Function.ParseJSON(response).accessToken;

  url = "https://serverurl.rest.marketingcloudapis.com/asset/v1/content/assets/";
  url += "123";
  var headerNames = ["Authorization"];
  var headerValues = ["Bearer " + accessToken];
  payload = '';

  var result = HTTP.Post(url, contentType, payload, headerNames, headerValues);

   var res =  result.Response.toString();
   var json = Platform.Function.ParseJSON(res);
   var items = json.items;
   for(var i = 0; i < items.length; ++i) {
     var itr = items[i];
     var cust_key = itr["customerKey"];
     Write(cust_key);
  }



} catch(e) {
  Write(e);
}

The code above returns the following:

System.Collections.Generic.Dictionary`2[System.String,System.Object]

The customerKey attribute is in the parent node of the JSON object.

0

Updated to show full script

<script runat = server >
Platform.Load("Core", "1");
try {
  var url = 'https://auth.exacttargetapis.com/v1/requestToken';
  var contentType = 'application/json';
  var request = {
    "clientId": "XXXXXXXXXXX",
    "clientSecret": "XXXXXXXXXXXXX"
  };
  var payload = Platform.Function.Stringify(request);
  var result = HTTP.Post(url, contentType, payload);
  var aRes = Platform.Function.ParseJSON(result.Response.toString());
  var auth = aRes.accessToken;

  var imageurl = 'https://www.exacttargetapis.com/asset/v1/content/assets/query';
  var contentType = 'application/json';
  var imagerequest = {
    "query": {
      "property": "id",
      "simpleOperator": "equals",
      "valueType": "int",
      "value": 123
    }
  };
  var imagepayload = Platform.Function.Stringify(imagerequest);
  var headerNames = ["Authorization"];
  var headerValues = ["Bearer " + auth];
  var imageresult = HTTP.Post(imageurl, contentType, imagepayload, headerNames, headerValues);
  var res = imageresult.Response.toString();
  var json = Platform.Function.ParseJSON(res);
  var items = json.items;
  for (var i = 0; i < items.length; ++i) {
    var itr = items[i];
    var cust_key = itr["customerKey"];
    Write(cust_key);
  }
} catch (e) {
  Write(Stringify(e));
} 
</script>
  • I still get the same error, I have updated the code to incorporate your suggested lines. – Deployment Failure Jun 17 at 16:19
  • 1
    This is because you aren't stringifying your catch response. My guess is that throws an error that you could action on (probably unable to retrieve security descriptor.) Updating my answer now to show a full working example of pulling content builder assets by ID on a landing page. – Jason Hanshaw Jun 17 at 16:41
  • 1
    You're right! It was an issue with the exception. – Deployment Failure Jun 17 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.