question

Felix .Ng avatar image
Felix .Ng asked

Can not parse json object,

Hi, I've tried to use CloudScript to parse to an JSON object but it didn't work anymore. I want a JSON object but always get the string.

Note that the "inventory" in my code is my own custom inventory, not the Playfab built-in inventory

Typescript

        let inventoryInternalData = server.GetUserInternalData({
            PlayFabId:currentPlayerId,
            Keys: ["inventoryKey"]
        }).Data;
        try {
             response.inventory = JSON.parse(JSON.stringify(inventoryInternalData)); 
        } catch (e) {
            log.debug(e);
        }

Internal json object

{
  "myFile.txtInventory": {
    "__type": "Gokyo.Inventory.ItemCollectionSerializationModel,Assembly-CSharp",
    "value": {
      "items": [
        {
          "itemID": 12,
          "amount": 1,
          "collectionName": "Inventory",
          "upgradable": true,
          "currentLevel": 22,
          "rarityID": 0
        },
        {
          "itemID": 12,
          "amount": 1,
          "collectionName": "Inventory",
          "upgradable": true,
          "currentLevel": 10,
          "rarityID": 0
        },
        {
          "itemID": 1,
          "amount": 2,
          "collectionName": "",
          "upgradable": false,
          "currentLevel": 0,
          "rarityID": -1
        }
      ],
      "currencies": [
        {
          "currencyID": 0,
          "amount": 4444
        },
        {
          "currencyID": 1,
          "amount": 0
        }
      ]
    }
  }
}

The return result response.inventory

 "inventory": {
            "inventoryKey": {
                "Value": "{\"myFile.txtInventory\":{\"__type\":\"Gokyo.Inventory.ItemCollectionSerializationModel,Assembly-CSharp\",\"value\":{\"items\":[{\"itemID\":12,\"amount\":1,\"collectionName\":\"Inventory\",\"upgradable\":true,\"currentLevel\":22,\"rarityID\":0},{\"itemID\":12,\"amount\":1,\"collectionName\":\"Inventory\",\"upgradable\":true,\"currentLevel\":10,\"rarityID\":0},{\"itemID\":1,\"amount\":2,\"collectionName\":\"\",\"upgradable\":false,\"currentLevel\":0,\"rarityID\":-1}],\"currencies\":[{\"currencyID\":0,\"amount\":4444},{\"currencyID\":1,\"amount\":0}]}}}",
                "LastUpdated": "2020-05-02T13:28:01.722Z",
                "Permission": "Private"
            }
        }

I've tried to parse with a json editor. It works fine

,

CloudScript
jsonparse.png (27.3 KiB)
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

·
Seth Du avatar image
Seth Du answered

I have confirmed it on my testing environment and here is some clarification.

The value that stored in Player Data is always a string (stringify JSON), which means when you retrieve the value via GetUserData API, you don’t have to stringify it again. It is possible that due to the twice stringify process, you need to parse twice (since you only parse once, it is still a stringified JSON). Hence, I believe you can refer to my code to solve this issue:

handlers.testfunc = function(args,context) {
    var request = {
      "PlayFabId": currentPlayerId,
      "Keys": [
        "inventoryKey"
      ]
    };
    var result = server.GetUserData(request);
    return JSON.parse(result.Data.inventoryKey.Value);
    
};



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

Felix .Ng avatar image Felix .Ng commented ·

Thanks for your respond.
I used to deploy the same way as you before, but it was not working as well.

Tried your code again, throwing an error message:

 "Error": {
        "Error": null,
        "Message": "There was a problem running testfunc. Check your arguments and try again.",
        "StackTrace": null
    }

If just JSON.parse(result.Data.inventoryKey) it throw this error:

"Error": {
        "Error": "JavascriptException",
        "Message": "JavascriptException",
        "StackTrace": "SyntaxError: Unexpected token o in JSON at position 1\n    at JSON.parse (<anonymous>)\n    at handlers.testfunc (59E5D-main.js:391:17)\n    at Object.invokeFunction (Script:116:33)"
    }
0 Likes 0 ·
Seth Du avatar image Seth Du ♦ Felix .Ng commented ·

How do you update/store this JSON data? Can you show your code? Please remember to hide personal information.

0 Likes 0 ·
Felix .Ng avatar image Felix .Ng Seth Du ♦ commented ·

For my test, I just paste the needed JSON in Player Data or Internal Data like this:

0 Likes 0 ·
storejson.png (25.4 KiB)
Show more comments
Seth Du avatar image Seth Du ♦ Felix .Ng commented ·

The stringify process should be along with parse. For the real cause of this issue, you may refer to: https://stackoverflow.com/questions/40878388/unexpected-token-o-in-json-at-position-1-issue-with-parsejson

0 Likes 0 ·
Felix .Ng avatar image Felix .Ng Seth Du ♦ commented ·

why I can not run your code?

First execute updatetest, it works fine. Then execute readtest, it throws

{
    "FunctionResult": null,
    "Logs": null,
    "ExecutionTimeSeconds": 0,
    "MemoryConsumedBytes": 0,
    "APIRequestsIssued": 0,
    "HttpRequestsIssued": 0,
    "Error": {
        "Error": null,
        "Message": "There was a problem running readtest. Check your arguments and try again.",
        "StackTrace": null
    }
}
0 Likes 0 ·
Show more comments
Show more comments

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.