question

luke1513 avatar image
luke1513 asked

How can i access json keys that are "deep" inside another key (child?) through cloud script

I am a beginner. I tried so many searches online and through the examples provided here but i just can't find anything. Maybe i am doing something wrong so there is no solution for it. I am trying to access, for example, first element of the Levels array, and then MinCA. I have tried so many times that i have reached i high revision number (btw how can i reset that? in the future it will be very high. is there a limit?). I tried to narrow it down to as basic as possible. Here is my last attempt and the JSON:

var areaData = server.GetTitleInternalData({
        Keys: [areaName]
    });
    
    var levelInfo2 = areaData.Data[areaName];
    var levelInfo1 = levelInfo2.Data[Levels[1]];
    var levelInfo = levelInfo1.Data["MinCA"];
{
  "Snacks": {
    "Levels": [
      {
        "Level": 1,
        "MinCA": 10,
        "MaxCA": 20,
        "UpgradeCost": 100
      },
      {
        "Level": 2,
        "MinCA": 15,
        "MaxCA": 25,
        "UpgradeCost": 200
      },
      {
        "Level": 3,
        "MinCA": 20,
        "MaxCA": 30,
        "UpgradeCost": 300
      }
    ]
  },
  "Drinks": {
    "Levels": [
      {
        "Level": 1,
        "MinCA": 5,
        "MaxCA": 10,
        "UpgradeCost": 50
      },
      {
        "Level": 2,
        "MinCA": 10,
        "MaxCA": 15,
        "UpgradeCost": 100
      },
      {
        "Level": 3,
        "MinCA": 15,
        "MaxCA": 20,
        "UpgradeCost": 150
      }
    ]
  },
  "Coffee": {
    "Levels": [
      {
        "Level": 1,
        "MinCA": 1,
        "MaxCA": 5,
        "UpgradeCost": 25
      },
      {
        "Level": 2,
        "MinCA": 5,
        "MaxCA": 10,
        "UpgradeCost": 50
      }
    ]
  }
}
CloudScriptTitle Data
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

·
Sarah Zhang avatar image
Sarah Zhang answered

You can refer to the following code and the attached Title Internal Data JSON(which can be uploaded as a file). JSON string needs to be parsed to JSON object to get its children items.

handlers.TestGetinternalTitleData = function (args) {

    var areaInfoValue = server.GetTitleInternalData({
        Keys: ["areaInfo"]
    });

    var areaInfoObject = JSON.parse(areaInfoValue.Data.areaInfo);
    var snackInfo = areaInfoObject.Snacks;
    var snackLevelsInfo = areaInfoObject.Snacks.Levels;
    var snackLevelOneInfo = areaInfoObject.Snacks.Levels[0];

    return { areaInfoObject: areaInfoObject, snackInfo: snackInfo, snackLevelsInfo: snackLevelsInfo, snackLevelOneInfo, snackLevelOneInfo };
};

{
                
"areaInfo":"{\"Snacks\":{\"Levels\":[{\"Level\":1,\"MinCA\":10,\"MaxCA\":20,\"UpgradeCost\":100},{\"Level\":2,\"MinCA\":15,\"MaxCA\":25,\"UpgradeCost\":200},{\"Level\":3,\"MinCA\":20,\"MaxCA\":30,\"UpgradeCost\":300}]},\"Drinks\":{\"Levels\":[{\"Level\":1,\"MinCA\":5,\"MaxCA\":10,\"UpgradeCost\":50},{\"Level\":2,\"MinCA\":10,\"MaxCA\":15,\"UpgradeCost\":100},{\"Level\":3,\"MinCA\":15,\"MaxCA\":20,\"UpgradeCost\":150}]},\"Coffee\":{\"Levels\":[{\"Level\":1,\"MinCA\":1,\"MaxCA\":5,\"UpgradeCost\":25},{\"Level\":2,\"MinCA\":5,\"MaxCA\":10,\"UpgradeCost\":50}]}}"
}

>> how can I reset the CloudScipt revisions?

We can remove the unused revisions by using the [REMOVE REVISION] button. When you select an unlived revision, you can find this button In the upper right of the page. Currently, we only can remove the revisions one by one.

2 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.

luke1513 avatar image luke1513 commented ·

Thank you very much for the reply. I think all my tries have failed because I didn't use JSON.parse. Had no idea i had to.

0 Likes 0 ·
luke1513 avatar image luke1513 commented ·

@Sarah Zhang Hey. Sorry to come here again but i tried the method and it gives me this error: "StackTrace": "SyntaxError: Unexpected token u in JSON at position 0\n at JSON.parse (<anonymous>)\n at handlers.AreaUpdate (25A5-main.js:72:27)".This is my code

var areaData = server.GetTitleInternalData({
        Keys: ["ShopAreas"]
    });
    
    var areaObject = JSON.parse(areaData.Data.ShopAreas);
<br>

Also, i wrapped my title data in the "ShopAreas" too.

Edit: Fixed

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.