question

katiepearce avatar image
katiepearce asked

Nested Json data in Cloudscript

I'm having a lot of trouble deserializing a nested Json on the server-side.

I have a portion of a script like this;

var titleData = server.GetTitleInternalData({"Keys" : loc})
  var campaignData = {};
  if (titleData.Data.hasOwnProperty(loc)){
    campaignData = titleData.Data[loc];
  }
  log.info(campaignData+"  "+campaignData[window]+" "+campaignData.hasOwnProperty(window));

(loc is the internal data key - currently "hearts_oldlibrary" in my test)

the result I get from that log.info statement is this;

{"oldlibrary":1}  undefined false

I'm expecting campaignData[window] to return 1, not undefined. (Window is being passed in args and is "oldlibrary".

I hope you can answer quickly - I need this app submitted and approved by the 13th but something that should be straightforward isn't being straightforward. D:

CloudScriptdata
1 comment
10 |1200

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

katiepearce avatar image katiepearce commented ·
I should probably show this bit so we can rule out typos;


handlers.addHearts = function (args, context) { log.info(args); log.info("two"); var campaign = ""; var window = ""; var amount = 0; if (args && args.campaign && args.window && args.amount){ log.info("three"); campaign = args.campaign; window = args.window; amount = args.amount; log.info("four"); } else return "Invalid arg";
0 Likes 0 ·

1 Answer

·
JayZuo avatar image
JayZuo answered

PlayFab stores all Title Data Values as string, so your nested Json data is also converted to string. If you looked at GetTitleInternalData method, you will also find the Data it returned is a dictionary object which both key and value are string type. So the campaignDatayou've got is a string and to make "campaignData[window]", you will need firstly parse it to JSON like the following:

    if (titleData.Data.hasOwnProperty(loc)) {
        campaignData = JSON.parse(titleData.Data[loc]);
    }
    log.info(JSON.stringify(campaignData) + "  " + campaignData[window] + " " + campaignData.hasOwnProperty(window)); 
3 comments
10 |1200

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

katiepearce avatar image katiepearce commented ·

Thanks. I had a feeling it might be something like that but sometimes I have no idea what methods exist in CloudScript since there is no context sensitive autocomplete to guide me the way visual studio does =Z

0 Likes 0 ·
brendan avatar image brendan katiepearce commented ·

I'd suggest having a look at using TypeScript - https://blog.playfab.com/blog/typescript2

0 Likes 0 ·
katiepearce avatar image katiepearce brendan commented ·

Thank you so much! I will definitely check that out when I'm not swamped with this launch. Looks like exactly what I need.

0 Likes 0 ·

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.