Utility Functions and C Language Reference > Utility Functions: C Language (lr_) > Alphabetical Listing > lr_json_replace


Replaces a node in a JSON object.

C Language

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

Example String and Parameter Functions


Name Comments
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 replaced.

The key-value pair to replace the target of the QueryString. Both the key and the value of the node that matches the query are replaced. The key-value pair can be bracketed or not. For example: "Value={\"color\":\"red\"}" or "Value=\"color\":\"red\"".

ValueParam The name of the parameter containing the key-value pair to replace the target of the QueryString.
JsonNode The name of a parameter containing the handle of the JSON object created by lr_eval_json The object referenced by JsonNode replaces the QueryString match in 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

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_replace replaces the node or nodes specified by the query with the key-value pair passed in the Value, ValueParam, or JsonNode arguments.

Use lr_json_replace to change the name and value of the matched node. If you want to change the value but not the name, you can use lr_json_set_values.

If more than one key-value pair is passed in Value or ValueParam, only the first is used.

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

Return Values

The number of matches.


Parameterization is not applicable to this function.


This script shows the usage of lr_json_replace.

	int i = 0;
	// See File: store.json
	                 "JsonObject=r_json_obj", LAST);

	/* Change "name": "Black Books" to "owner":"Mr Black".*/
	i = lr_json_replace("JsonObject=r_json_obj",
	                "Value=\"\owner\":\"Mr Black\"",

	if (i != 1)
		lr_error_message("lr_json_replace should return %d, but returns %d", 1, i);
	lr_save_string("$.store.books[*].isbn", "r_path_isbn");
	i = lr_json_replace("JsonObject=r_json_obj",
	                "Value=\"discount\":\"20 percent\"",
	if (i != 2)
		lr_error_message("lr_json_replace should return %d, but returns %d", 2, i);

	return 0;

TestReplace.c(40): lr_json_replace succeed, 1 match

TestReplace.c(50): Notify: Saving Parameter "r_path_isbn = $.store.books[*].isbn".

TestReplace.c(52): Notify: Parameter Substitution: parameter "r_path_isbn" = "$.store.books[*].isbn"

TestReplace.c(52): lr_json_replace succeed, 2 matches

TestReplace.c(60): Notify: Saving Parameter "r_result = {"store":{"owner":"Mr Black","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","discount":"20 percent","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","discount":"20 percent","price":22.99}]}}".