question

brendan avatar image
brendan asked

Embedded JSON object as data

Question from a developer:

I have a lot of little data items to store in user data, so I'd like to group things up so that they take fewer keys. Can you provide some example code showing how to do this?

 

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

·
brendan avatar image
brendan answered

Certainly! Here are two simple examples showing how to use JSON stringify and parse to quickly transform complex JSON objects to strings, which you can then use as the Value in a Key/Value pair, and then deseriallize it back to a JSON object.

Take a JSON object, turn it into a string, and write it to User Read Only Data (the Key is "EmbeddedJson" for both examples):

var jsonObject = { Name: "foo", Level: 3, Type: "thing", Stuff: [ 10, 12, 4, 2 ] };
var jsonString = JSON.stringify(jsonObject);
var response = server.UpdateUserReadOnlyData( { PlayFabId: currentPlayerId, Data: { EmbeddedJson: jsonString } } );

Read out an encoded string and turn it back into a JSON object:

var response = server.GetUserReadOnlyData( { PlayFabId: currentPlayerId, Keys: [ "EmbeddedJson" ] } );
var reconstructedJSON = JSON.parse( response.Data.EmbeddedJson.Value );

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.