0

I am using the SSJS Core Library HTTP.Post function to retrieve an SFMC authentication token. I have followed ssjs boilerplate from Zuzanna here: https://gist.github.com/zuzannamj/40f01efd553a08335c9d73c444fe80df#file-sfmc-api-v1-authentication-ssjs-js

The post function response is a JSON object with:

  • StatusCode
  • Response array with a stringified JSON object inside

Why is the object inside the response array a string?

This then requires using Platform.Function.ParseJSON(accessTokenRequest.Response[0]) to make it usable and assign variables, like in the github example code above.

If we were to Write(Stringify(accessTokenRequest)); the example API Response is below. Notice the slash character escape which indicates its a string.

{"StatusCode":200,"Response":["{\"access_token\":\"eyFdolvdfverv54544rbvf454SzpfzLpYDeOpF\",\"token_type\":\"Bearer\",\"expires_in\":1038,\"scope\":\"data_extensions_read data_extensions_write\",\"soap_instance_url\":\"https://xxx.soap.marketingcloudapis.com/\",\"rest_instance_url\":\"https://xxx.rest.marketingcloudapis.com/\"}"]}

2

Reponse[0] in your example is indeed a string. It could contain HTML, XML... whatever. In your example, it contains a string containing JSON. If you parse Reponse[0] you get a JavaScript object. If you Stringify Reponse[0], you get an escaped string (you're stringifying a string). If you Stringify the parsed Reponse[0] (JavaScript object), you get unescaped JSON:

Write(Stringify(Platform.Function.ParseJSON(accessTokenRequest.Response[0])))

The output would look similar to this:

{
  "StatusCode": 200,
  "Response": [{
    "access_token": "eyFdolvdfverv54544rbvf454SzpfzLpYDeOpF",
    "token_type": "Bearer",
    "expires_in": 1038,
    "scope": "data_extensions_read data_extensions_write",
    "soap_instance_url": "https: //xxx.soap.marketingcloudapis.com/",
    "rest_instance_url": "https://xxx.rest.marketingcloudapis.com/"
  }]
}
4
  • Cheers @Macca ! You've said in an easy to understand fashion, thank you. At the time I didn't actually realise (until you said) that I was stringifying a string. Do you know why the Response[0] is in fact a STRING that contains JSON (instead of a ready to go JavaScript object) ?
    – charliefay
    Sep 21 '20 at 8:10
  • 1
    As I said, what the remote server returns could be anything - maybe a pdf, a csv file or something else. If you're used to writing code in something like Node.js, you'll avail of middleware like body-parser to intercept the received stream and parse the content for you. SSJS doesn't do this for you. It just delivers you the content (as a string) and you have to figure out what to do with it, just like you would have to do with barebones Node.
    – Macca
    Sep 21 '20 at 10:01
  • On a related but slightly off-topic question, can you please confirm if the Results[0] property in a WS Proxy retrieve request returns an Object and therefore parseJson is not needed (because its not a string)?
    – charliefay
    Sep 21 '20 at 12:49
  • The response contains a string that you'd need to parse to get an object.
    – Macca
    Sep 21 '20 at 23:24

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.