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

lr_json_delete

Deletes the values in a JSON object that match the query.

C Language

int lr_json_delete( "JsonObject=<parameter name>", QueryString=<JSON query>", ["SelectAll=<Yes|No>",] ["NotFound=<Error|Continue>",] LAST );

Example String and Parameter Functions

Arguments

Name Comments
JsonObject The name of an object created with lr_eval_json .
QueryString The JSON query
NotFound

Optional. One of:

  • Error - Script run is aborted and error message is sent. (Default)
  • Continue - Script run continues. A warning message is sent.
SelectAll Optional: If Yes, deletes all matches. If No, deletes first match. Default is No.
LAST Required marker for the end of the argument list.

lr_json_delete deletes items from a JSON object that match the query.

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

Return Values

The number of items deleted.

Parameterization

Parameterization is not applicable to this function.

Example

This script shows the usage of lr_json_delete.

TestDelete()
{
	int i = 0;
	
	// See File: store.json
	lr_eval_json("Buffer/File=store.json", 
	                 "JsonObject=d_json_obj", LAST);
	
	lr_json_stringify("JsonObject=d_json_obj",
	                  "OutputParam=d_result",
	                 LAST);
	// 1 match
	i = lr_json_delete("JsonObject=d_json_obj",
	                "QueryString=$.store.name",
	                 LAST);
	lr_json_stringify("JsonObject=d_json_obj",
	                  "OutputParam=d_result",
	                 LAST);
	if (i != 1)
		lr_error_message("lr_json_delete should return %d, but returns %d", 1, i);
	
	lr_save_string("$.store.books[*].isbn", "d_path_isbn");
	
	// 2 matches
	i = lr_json_delete("JsonObject=d_json_obj",
	                "QueryString={d_path_isbn}",
	                "SelectAll=Yes",
	                 LAST);
	if (i != 2)
		lr_error_message("lr_json_delete should return %d, but returns %d", 2, i);
	
	lr_json_stringify("JsonObject=d_json_obj",
	                  "OutputParam=d_result",
	                 LAST);
	
	return 0;
}

TestDelete.c(9): Notify: Saving Parameter "d_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}]}}".

TestDelete.c(13): lr_json_delete succeed, 1 match

TestDelete.c(16): Notify: Saving Parameter "d_result = {"store":{"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}]}}".

TestDelete.c(22): Notify: Saving Parameter "d_path_isbn = $.store.books[*].isbn".

TestDelete.c(25): Notify: Parameter Substitution: parameter "d_path_isbn" = "$.store.books[*].isbn"

TestDelete.c(25): lr_json_delete succeed, 2 matches

TestDelete.c(32): Notify: Saving Parameter "d_result = {"store":{"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","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","price":22.99}]}}".