lr_json_set_values

Changes values in a JSON object.

C Language

int lr_json_set_values( "JsonObject=<parameter name>", "Value or ValueParam=<value>","QueryString=<query>", ["SelectAll=<Yes|No>",] ["NotFound=<Error|Continue>",] LAST );

ExampleParameter Functions

Arguments

NameComments
JsonObjectThe name of the parameter that stores the handle of the JSON object created by lr_eval_json.
QueryStringThe JSON query.
ValueThe data to set
ValueParam

The name of a parameter containing the data to set. If SelectAll = Yes, pass a parameter array with the same number of values as the expected number of matches.

SelectAllOptional: If Yes, finds all matches. If No, stops after first match. Default is No.
NotFound

Optional. One of:

  • Error - Script run is aborted and error message is sent. (Default)
  • Continue - Script run continues. A warning message is sent.
LAST Required marker for the end of the argument list.

lr_json_set_values sets the values at the nodes that match the query to the specified value.

If a parameter array is passed as the ValueParam and SelectAll = Yes, the values in the array are used in the corresponding matches. That is, the first value in the parameter array is the new value for the first match, the second value is for the second match, and so on. If there are more matches than there are elements in the parameter array, the value replacement stops when all the array elements have been used. For example, if the parameter array contains 5 elements and there are 10 matches to the query, the first 5 matches have the values replaced with the 5 array elements, respectively, and the remaining matches are not changed.

If you want to change the key name as well as the value, use lr_json_replace.

This function is not recorded. You can insert it manually into your script.

Return Values

The number of matches.

Parameterization

Parameterization is not applicable to this function.

Example

This script shows the usage of lr_json_set_values.

TestSetGetValues()
{
	int i = 0;
	
	// Test Set/Get Values
	
	i = lr_eval_json("Buffer/File=store.json", // See File: store.json
	                 "JsonObject=s_json_obj", LAST);
	
	lr_json_stringify("JsonObject=s_json_obj",
	                  "OutputParam=s_result",
	                 LAST);
	
	// Single match
	i = lr_json_set_values("JsonObject=s_json_obj",
	                "Value=\"\Red Books\"",
	                "QueryString=$.store.name",
	                 LAST);	
	
	lr_json_stringify("JsonObject=s_json_obj",
	                  "OutputParam=s_result",
	                 LAST);
	
	if (i != 1)
		lr_error_message("lr_json_set_values should return %d, but returns %d", 1, i);
	
	// Test Get Values
	i = lr_json_get_values("JsonObject=s_json_obj",
                "ValueParam=s_new_storename",
                "QueryString=$.store.name",
                 LAST);
	
	if (i != 1)
		lr_error_message("lr_json_get_values should return %d, but returns %d", 1, i);
	
	// Multi match with SelectAll=No
	i = lr_json_set_values("JsonObject=s_json_obj",
	                "Value=\"Ellie\"",
	                "QueryString=$.store.books[*].author",
	                 LAST);	
	if (i != 1)
		lr_error_message("lr_json_set_values should return %d, but returns %d", 1, i);

	i = lr_json_get_values("JsonObject=s_json_obj",
                "ValueParam=s_new_firstauthor",
                "QueryString=$.store.books[*].author",
                 LAST);
	
	if (i != 1)
		lr_error_message("lr_json_get_values should return %d, but returns %d", 1, i);
	
	// Multi match with SelectAll=Yes
	lr_save_string("\"1111\"", "s_isbn_1"); 
	lr_save_string("\"2222\"", "s_isbn_2"); 
	lr_save_string("2", "s_isbn_count");

	lr_save_string("$.store.books[*].isbn", "s_path_isbn");
	
	i = lr_json_set_values("JsonObject=s_json_obj",
	                "ValueParam=s_isbn",
	                "QueryString={s_path_isbn}",
	                "SelectAll=Yes",
	                 LAST);	
	if (i != 2)
		lr_error_message("lr_json_set_values should return %d, but returns %d", 2, i);	

		
	i = lr_json_get_values("JsonObject=s_json_obj",
                "ValueParam=s_new_isbn",
                "QueryString={s_path_isbn}",
	            "SelectAll=Yes",
                 LAST);
	if (i != 2)
		lr_error_message("lr_json_get_values should return %d, but returns %d", 2, i);
	
	
	lr_json_stringify("JsonObject=s_json_obj",
	                  "OutputParam=s_result",
	                 LAST);
	
	return 0;
}
        

TestSetGetValues.c(7): lr_eval_json succeed

TestSetGetValues.c(10): Notify: Saving Parameter "s_result = {"store":{"name":"Black Books","address":"No 20, J Rd","books":[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}]}}".

TestSetGetValues.c(15): lr_json_set_values succeed, 1 match

TestSetGetValues.c(20): Notify: Saving Parameter "s_result = {"store":{"name":"Red Books","address":"No 20, J Rd","books":[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}]}}".

TestSetGetValues.c(28): Notify: Saving Parameter "s_new_storename = Red Books".

TestSetGetValues.c(28): lr_json_get_values succeed, 1 match

TestSetGetValues.c(37): lr_json_set_values succeed, 1 match

TestSetGetValues.c(44): Notify: Saving Parameter "s_new_firstauthor = Ellie".

TestSetGetValues.c(44): lr_json_get_values succeed, 1 match

TestSetGetValues.c(53): Notify: Saving Parameter "s_isbn_1 = "1111"".

TestSetGetValues.c(54): Notify: Saving Parameter "s_isbn_2 = "2222"".

TestSetGetValues.c(55): Notify: Saving Parameter "s_isbn_count = 2".

TestSetGetValues.c(57): Notify: Saving Parameter "s_path_isbn = $.store.books[*].isbn".

TestSetGetValues.c(59): Notify: Parameter Substitution: parameter "s_path_isbn" = "$.store.books[*].isbn"

TestSetGetValues.c(59): lr_json_set_values succeed, 2 matches

TestSetGetValues.c(68): Notify: Parameter Substitution: parameter "s_path_isbn" = "$.store.books[*].isbn"

TestSetGetValues.c(68): Notify: Saving Parameter "s_new_isbn_1 = 1111".

TestSetGetValues.c(68): Notify: Saving Parameter "s_new_isbn_2 = 2222".

TestSetGetValues.c(68): Notify: Saving Parameter "s_new_isbn_Count = 2".

TestSetGetValues.c(68): lr_json_get_values succeed, 2 matches

TestSetGetValues.c(77): Notify: Saving Parameter "s_result = {"store":{"name":"Red Books","address":"No 20, J Rd","books":[{"category":"reference","author":"Ellie","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"1111","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"2222","price":22.99}]}}".