The JsonSetValue function uses a JSONPath expression to change the value of a specific path.
int @JsonSetValue(string objectId, string jsonPath, object value);
int @JsonSetValue(string objectId, string jsonPath, object value, string dataType);
Parameters
string objectId : Target JSON
object ID
string jsonPath : JSONPath expression
to set the value
object value
: a value to set
string dataType : The data type of
the value, "string" when not filled
"string": string
"int", "long" : integer
"float", "double", "decimal": real
"bool" : boolean
"datetime" : datetime
"null" : null
Return Values
1 : Success
0 : Failure
Note. JSONPath Expression Examples
$.name
: The "name" attribute of the top-level object
$.address.city
: The "city" property of the nested object
$.items[0]
: the first element of the array
$.items[*]
: All array elements
$.. name
: All "name" attributes in the object tree
$.items[?( @.price>10)]
: Array element that matches the condition (element whose price is greater than
10)
Example )
//Assume the JSON string below is the
current value of string jsonString
{
"person": {
"name":
"John",
"contacts": [
{"type": "phone", "value": "010-1234-5678"},
{"type": "email", "value": "John@example.com"}
]
}
}
string jsonObject = @JsonFromString(jsonString);
// Changing the value
using JSONPath
@JsonSetValue(jsonObject, "$.person.name", "Patrick", "string");
@JsonSetValue(jsonObject, "$.person.contacts[0].value", "010-9876-5432",
"string");
string result = @JsonToString(jsonObject);
//Changed jsonString value
{
"person": {
"name": "Patrick",
"contacts": [
{"type": "phone",
"value": "010-9876-5432"},
{"type": "email", "value": "John@example.com"}
]
}
}
Version information
Supported Version: 10.3.6.25 or later
Related Helps)
JsonTemplateReplacePlaceholder