The UpdateJSON() method calls an API with the update operation enabled and update the JSON data of a specific column using the provided new JSON data. |
Result The UniREST Server application will update the given column JSON value with the given JSON data, using specific updating rules. Then, it will reply with a simple confirmation. Since this operation requires identifying a specific, existing record inside a table, and a specific column, you must provide the necessary Keys and column name. |
This method offers a very easy and practical way to update columns value that are in JSON format.
_ = #UniRESTClient.Async#.$UpdateJSON$(%api, tableData, jsonData, formula, callBack%);
api|string
The API address, under the API class.
__
tableData|object
Optional values to send to the server. It must be an instance of the table class in use.
__
fieldToUpdate|string
The name of the specific column to update. It must be a numeric value (FLOAT).
__
jsonData|Json
A UniREST JSON data object.
__
callBack|Action<bool>
The function to call on operation completed.
bool
true if everything is ok, false if an error occurred.
jsonData parameter
The jsonData parameter must be a data object created by the built-in UniREST JSON class. If the Database table’s record column is empty, the JSON data will be simply stored. Instead, if it already contains a JSON value, the updating procedure will follow these rules:
- existing JSON keys will be updated with the new values;
- new JSON keys will be added;
Example
jsonData | Current DB value | What will happen | DB value after UpdateJSON() |
{ ……“name” : “Conan”, ……“health” : 1000 } | The DB doesn’t contain a value. The jsonData is just saved. | { ……“name” : “Conan”, ……“health” : 1000 } | |
{ ……“health” : 800 } | { ……“name” : “Conan”, ……“health” : 1000 } | The DB contains JSON data. The jsonData has a new value of the “health” key. The DB is updated with the new value of “health”. | { ……“name” : “Conan”, ……“health” : 800 } |
{ ……“health” : 1200, ……“gold” : 5000 } | { ……“name” : “Conan”, ……“health” : 800 } | The DB contains JSON data. The jsonData has a new value of the “health” key and the new “gold” key. The DB is updated with: – the new value of “health”; – the new “gold” key. | { ……“name” : “Conan”, ……“health” : 1200, ……“gold” : 5000 } |
UpdateJSON() example
The following C# example replicates the row 3 case of the table above.
var myJData = new Json();
myJData.Add("health", 1200);
myJData.Add("gold", 5000);
_ = UniRESTClient.Async.UpdateJSON(
API.player_data,
new DB.Players
{
id = UniRESTClient.UserID
},
"player",
myJData,
(bool ok) =>
{
// Some code here...
}
);