Sets a member within a JSON object or element within a JSON array to the given value.
Syntax
Result = SRP_Json(Handle, "SetValue", Identifier, NewValue, Type)
Returns
Returns 1 if successful, 0 if not.
Parameters
Parameter | Description |
---|---|
Handle | Handle to a JSON Entity. Required. |
NewValue | New value to be added to the JSON array. Required. |
Type | The value's type: String, Number, or Boolean. Optional. |
Remarks
The SetValue service sets a child within a given JSON Entity. This service only works on JSON objects and JSON arrays. If it is a JSON object, then Identifier needs to be a member name, such as "id" or "first-name". If it is a JSON array, then identifier needs to be an index, such as 1 or 10. Unlike Set, you do not pass another JSON Entity Handle. Instead, you pass a value via the NewValue parameter. When you pass the value without specifying its type, then its type is automatically determined from the value itself. Empty values become Null types and values detected to be a number become the Number type. Everything else becomes a string. You can always force the type you want by setting the Type parameter explicitly.
It does not matter if the member/element already exists or not. Calling this service will either create the member/element if necessary or simply replace what's already there. For arrays, it does not matter how big the array currently is as the array will always grow in order to include the desired index. For example, if you have an array with 10 elements and then set element 25 to another entity, then the array will become 25 in size with elements 11-24 being defaulted to null.
Example
// Create a JSON array, add elements to it, and get the count If SRP_Json(ArrayHandle, "New", "Array") then SRP_Json(ArrayHandle, "SetValue", 1, "12345") SRP_Json(ArrayHandle, "SetValue", 10, 67890, "Number") SRP_Json(ArrayHandle, "SetValue", 5, 1, "Boolean") SRP_Json(ArrayHandle, "Release") end // Create a JSON object, add members to it, and get the count If SRP_Json(ObjectHandle, "New", "Object") then SRP_Json(ObjectHandle, "SetValue", "name", "John Doe") SRP_Json(ObjectHandle, "SetValue", "city", "Washington D.C.") SRP_Json(ObjectHandle, "SetValue", "zip", "12345", "Number") SRP_Json(ObjectHandle, "Release") end