0

For a cloud page, I have a use case, where using the Script.Util.HttpRequest/Response I get the OAuth token.

Then I have another request, the response structure of which looks like below:

{
  "requestId": "xyz",
  "definitions": [
    {
      "definitionKey": "account-reset"
    },
    {
      "definitionKey": "account-submit"
    }
  ],
  "count": 1,
  "page": 1,
  "pageSize": 10
}

I am getting the response, however, the response string length is always 15 chars -> should be more and the Platform.Function.ParseJSON returns null.

Greatly appreciate any help and some sample code. I don't want to use the HTTP.Get and HTTP.Post unless they are the last resort. Below is my code snippet causing the issue.

var resp = req.send();// this is the Script.Util function for HTTP Send
var resultString = resp.content;
var resultJSON = Platform.Function.ParseJSON(resultString);
2
  • What happens when you wrap the parsing in a try/catch? – Adam Spriggs Jan 5 at 0:14
  • When I put it in try catch, this is the error it is showing: {"message":"Unable to retrieve security descriptor for this frame.","description":"System.InvalidOperationException: Unable to retrieve security descriptor for this frame. - from mscorlib\r\n\r\n"} – rajpc Jan 5 at 0:18
3

Script.Util returns in a CLR format which does not translate well to SSJS. You need to first turn this CLR object to a string format via String() then use ParseJSON() to turn it back into an object as a JSON for you to utilize and parse through.

E.g.

Var myObj = Platform.Function.ParseJSON(String(resp.content))

For more info see my blog article here or a video I did on it here.

8
  • I did the following: var resp = req.send(); var resultString = String(resp.content); var respJSON = Platform.Function.ParseJSON(resultString); // this no longer gives an exception but the 'respJSON' is blank – rajpc Jan 5 at 3:42
  • I added the following lines of code var content = resp.content; var contentType = resp.contentType; var encoding = resp.encoding; var status = resp.returnStatus; var code = resp.statusCode;. The results are as follows: { "requestId": "aef43135","definitions": [ { "definitionKey": "2caf3954", "createdDate": "2020-12-22T15:41:00", "modifiedDate": "2020-12-22T16:16:00" } ], "page": 1, "pageSize": 50 } contentType : application/json; charset=UTF-8 Encoding : status : 0 code : undefined All I need is "definitionKey" – rajpc Jan 5 at 13:26
  • I printed each variable above using the "Write" function – rajpc Jan 5 at 13:33
  • Sorry, one more comment - Is there a way that we get the values from SSJS and then use the browser side JS libraries like underscore.js or something to parse the SSJS JSON response. Maybe use a dummy DE to write the JSON response and later use underscore.js to parse the JSON. I don't know how the code might look like but is there such an option. And I want all of this to happen when the cloud page loads – rajpc Jan 5 at 13:39
  • what you provided for 'definitions' is not a valid JSON, so if this is an accurate representation of your return, that could be causing an issue. Can you edit your question to include what you are currently doing and the expected response to allow for easier viewing? – Gortonington Jan 5 at 14:03
0

Response from Script.Util is not a string and that causes Platform.Function.ParseJSON() to fail. Wrap it with String() and it will work.

var resultJSON = Platform.Function.ParseJSON(String(resultString));

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.