lr_json_insert

Inserts a node in a JSON object.

C Language

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

ExampleParameter Functions

Arguments

NameComments
JsonObject The name of the parameter that stores the handle of the JSON object created by lr_eval_json.
QueryString The JSON path of the node to be inserted.
Value The string to insert. If the target node type is array, the value must be parsible into a JSON array. If the target node type is object, the value must be parsible into a JSON object.
ValueParamThe name of the parameter containing the string to insert.
JsonNodeThe name of a parameter containing the handle of the JSON object created by lr_eval_json The object referenced by JsonNode is inserted into the object referenced by JsonObject.
SelectAll Optional: If SelectAll=Yes, the Value is inserted at every location that matches the query. If SelectAll=No, the Value is inserted at the first location that matches the query. 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_insert inserts the value in the JSON object at the location specified by the query.

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_insert.

TestInsert()
{
	int i = 0;
	
	// See File: store.json
	lr_eval_json("Buffer/File=store.json", 
	             "JsonObject=i_json_obj", LAST);
	// 1 match
	i = lr_json_insert("JsonObject=i_json_obj",
	                "Value={\"owner\":\"Mr Black\"}",
	                "QueryString=$.store",
	                 LAST);	
	if (i != 1)
		lr_error_message("lr_json_insert should return %d, but returns %d", 1, i);
	
	// 1 match with JsonNode
	lr_save_string("{\"isbn\": \"0-048-08048-9\"}", "i_new_isbn");
	
	lr_eval_json("Buffer={i_new_isbn}", 
	             "JsonObject=i_json_obj2", LAST);
	
	i = lr_json_insert("JsonObject=i_json_obj",
	                   "JsonNode=i_json_obj2",
	                   "QueryString=$.store.books[0]",
	                   LAST);
	if (i != 1)
		lr_error_message("lr_json_insert should return %d, but returns %d", 1, i);
	
	
	// 2 match with ValueParam and SelectAll=Yes
	lr_save_string("{\"discount\":\"20 percent\"}", "i_new_discount");
	
	i = lr_json_insert("JsonObject=i_json_obj",
	                   "ValueParam=i_new_discount",
	                   "QueryString=$.store.books[?(@.price > 10)]",
	                   "SelectAll=Yes",
	                   LAST);
	if (i != 2)
		lr_error_message("lr_json_insert should return %d, but returns %d", 1, i);
	
	// 1 match
	// Notice that both of  the new items are inserted into the target array
	i = lr_json_insert("JsonObject=i_json_obj",
	                "Value=[{\"title\":\"New Book I\"}, {\"title\": \"New Book II\"}]",
	                "QueryString=$.store.books",
	                 LAST);	
	if (i != 1)
		lr_error_message("lr_json_insert should return %d, but returns %d", 1, i);
	
	lr_json_stringify("JsonObject=i_json_obj",
	                  "OutputParam=i_result",
	                 LAST);

	return 0;
}

TestInsert.c(9): lr_json_insert succeed, 1 match

TestInsert.c(17): Notify: Saving Parameter "i_new_isbn = {"isbn": "0-048-08048-9"}".

TestInsert.c(19): Notify: Parameter Substitution: parameter "i_new_isbn" = "{"isbn": "0-048-08048-9"}"

TestInsert.c(19): lr_eval_json succeed

TestInsert.c(22): lr_json_insert succeed, 1 match

TestInsert.c(31): Notify: Saving Parameter "i_new_discount = {"discount":"20 percent"}".

TestInsert.c(33): lr_json_insert succeed, 2 matches

TestInsert.c(44): Warning: The string '"title":"New Book I"' with parameter delimiters is not a parameter.

TestInsert.c(44): Warning: The string '"title": "New Book II"' with parameter delimiters is not a parameter.

TestInsert.c(44): lr_json_insert succeed, 1 match

TestInsert.c(51): Notify: Saving Parameter "i_result = {"store":{"name":"Black Books","address":"No 20, J Rd","books":[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95,"isbn":"0-048-08048-9"},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99,"discount":"20 percent"},{"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,"discount":"20 percent"},{"title":"New Book I"},{"title":"New Book II"}],"owner":"Mr Black"}}".

TestInsert.c(51): lr_json_stringify succeed