Sign In Register

How can we help you today?

Start a new topic

Sending JSON data to cloud code?

Hi, I am trying to save data in Unity3D from an json string using the LogEventRequest like this:


new GameSparks.Api.Requests.LogEventRequest().SetEventKey("UPDATE_PLAYER_DATA").SetEventAttribute("FROM", fromWhere).SetEventAttribute("VAL", JsonConvert.SerializeObject(GSDATA.instance.user)).Send((response) => {

 My cloud code looks like this:


var playerDataList = Spark.runtimeCollection("playerData"); 
var playerID = Spark.getPlayer().getPlayerId();

var from = Spark.getData().FROM;
var values = Spark.getData().VAL;

	"playerID": playerID
}, //Looks for a doc with the id of the current player
	"$set": values
}, // Uses the $set mongo modifier to set old player data to the current player data
true, // Create the document if it does not exist (upsert)
false // This query will only affect a single object (multi)

 I have created an Event like this:

The error I get back looks like this:


GS: RECV:{"@class":".GameSparksErrorResponse","error":{"message":"Wrapped com.mongodb.WriteConcernException: { \"serverUsed\" : \"gsp-XXXXXX-XXXX.gsp-aeu:XXXXX\" , \"ok\" : 1 , \"n\" : 0 , \"updatedExisting\" : false , \"err\" : \"Modifiers operate on fields but we found a String instead. For example: {$mod: {<field>: ...}} not {$set: \\\"{\\\"userId\\\............

 I understand that it sends it like a string and not json. When I do this in my Test Harness everything is going OK:


 "@class": ".LogEventRequest",
 "eventKey": "UPDATE_PLAYER_DATA",
 "FROM": "",
 "VAL": {"league":2,"qvip":15}

 How do I send the jsonobject from Unity to the cloud code without the cloud code threating it like a string?

Really hope on help in this matter and thanks in advance :-)

4 people have this question

Hi Jesper,

You can make use of JSON.parse and JSON.stringify to convert between Strings and Objects:


Hope that helps.



I'm also trying to send a JSON and save it in a collection, so I'm sending it as a string then converting it to JSON using  JSON.parse function in cloud code, but I'm getting the following error:

"error":{"message":"Function save does not accept parameters (object).

Here is my cloud code:

var collection = Spark.runtimeCollection("ThrustsInfo");
var json = JSON.parse(Spark.getData().ThrustInfo);;

Spark.setScriptData("JSON", JSON.stringify(json));

Also, I get a NullReference exception  when I try to access the JSON from the scriptData, using this code :


Can you help me in this issue? 


Hi Yaser,

You might try collection.insert(json) to insert a new document into the collection. Would you be able to provide your entire script for this request?

Kind regards,

 - Steve

Hi Yaser,

I've been attempting to replicate this error in my own game using the code provided, without success. Could you provide us with the string you're supplying in the 'ThrustInfo' attribute of your event?

The null reference exception would be occurring because your script is hitting an error and terminating before the scriptData gets set.



Hi, thanks for your response.

The ThrustInfo is an array, and I'm using library to convert it to a json string,  here is the json string:  


 I have also tried to use GSRequstData class then added the json to it:    

GSRequestData DATA = new GSRequestData();

Then I sent the DATA with setEventAttribute,  and it worked, but I couldn't send it back using the scriptData .   


Hi Yaser,

That might explain what's happening here. The save function is expecting a document (or json object) as opposed to an array of json objects. It's designed for performing an operation (either insert or update) on a single document. 

What exactly are you trying to accomplish? I'm assuming from the fact that the array elements you're passing don't have id fields that you're trying to create a new document for each array element? 

If this is the case you could use collection.insert(json), which will happily accept an array of objects and create a document for each.




Well, I want to have a document that contains an array of objects because I want to save the info of a match, and I want it to contain info about the players who were in the match. and this was just a test so I haven't added the id yet.

I know that I can create a document for each player but that will lead to too many documents, which may slow the process of retrieving them.

If you know a better way to accomplish that then I'd appreciate it if you can tell me.


Also, saving an array actually worked using the save function, the problem was only in getting the JSON by the scriptData.

Hi yaser,

The internal 'opponentsList' array isn't the problem, it's the actual array of matches being passed in "ThrustInfo". If, for example, you test this with just a single match:


it should work without issue as the save function expects an object as opposed to an object array. Does that make sense?




I didn't mean the internal array, I meant the object itself is an array, I saved it to a collection and it worked, but then I saved it as a dictionary with ID to each item and it worked too.

I can give you a screenshot from the NoSQL explorer if you want.

What I didn't know how to do is to send the JSON back in scriptData of the response, but I don't think I need it anymore.

But I have a question about getting the data from the collection:

can I get an item from the dictionary(JSON) by its ID in the cloud code? Like:

{ "ThrustInfo" :{ "id"} } 

I Think that should give me the item that has the specified ID right? 

Thanks and sorry for the Confusion.


here is the dictionary saved in a collection .


I'm trying to get a document from the collection based on an id in a dictionary in the document. 

For example, I have a dictionary in each document, and I want to find all Dictionaries that contain an item with id "3", How Can I do that?

I tried to use that in cloud code:

collection.find("DictionaryName" , "3");

Note: 3 here is the item id.

But that didn't work, I'm getting Null.

Can you help me with that? 

Thanks, and sorry for asking the question again.


I think I should create a new Topic for my question?

Hi Yaser,

Yes I think it might be best to start a new Topic. Would you be able to post a sample of the data in question and we may be able to help further.

Kind regards,

 - Steve

Login to post a comment