question

unbrokencraig avatar image
unbrokencraig asked

Parsing response inside Cloudscript

I figured out my previous question to make an external http call to Steam's WebAPI, but now I'm having trouble parsing the results inside the response.

If I return the response like so:

return { responseContent: response };

Then in the output of the CloudScript I see

"responseContent": "{\"appownership\":{\"ownsapp\":true,\"permanent\":false,\"timestamp\":\"2019-02-27T18:49:25Z\",\"ownersteamid\":\"XXXXXXXXXXXXXXX\",\"sitelicense\":false,\"result\":\"OK\"}}"

I want to access the timestamp value inside this, do a check against that value, then make a few PlayFab API calls depending on the result, but I have not been able to figure out a way to access that value from the result.

Looking through all the relevant questions and docs, it seems like I need to do something with JSON.parse, but I haven't had any success with that so far.

var responseObj = JSON.parse(response);
    
log.debug("test1: " + responseObj);

just printed out "Message": "test1: [object Object]", but I was expecting the JSON equivalent of the string printed above in "responseContent".

Is there a documentation page I'm missing that covers how to use JSON inside Cloudscript, because it seems like that would help significantly. Once I can get the value of "timestamp" into a var in CloudScript I think I should be able to do the rest of what I need pretty easily.

CloudScript
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Hernando avatar image
Hernando answered

To get the value of "timestamp", you can refer the following statement:

var timeStamp = JSON.parse(response).appownership.timestamp

Besides, in the log.debug, JavaScript will implicitly conversion the Object and call the toString() method on this custom object, it returns the default value inherited from Object: [object Object].

So if you printed out the content of this object, please refer this code:

log.debug("test1: " + JSON.stringify(responseObj, null, "\t"))
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.