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

lr_json_find

Returns the number of matches to a query in a JSON object.

C Language

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

Example String and Parameter Functions

Arguments

Name Comments
JsonObject The name of the parameter that stores the handle of the JSON object created by lr_eval_json.
QueryString The JSON query
Value The value to find.
ValueParam The name of a parameter containing the value to find
UseRegExp Optional: If Yes, the Value is a regular expression. Default is No.
SelectAll Optional: If Yes, returns the total number of matches. If No, stops after first match. Default is No.
IgnoreCase Optional: If Yes, ignore case. 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_find runs the query on the JSON object and returns the number of matches.

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

Return Values

The number of matches. If SelectAll = No, the number of matches is 0 or 1.

Parameterization

Parameterization is not applicable to this function.

Example

This script shows the usage of lr_json_find.

TestFind()
{
	int i = 0;
	
	// See File: store.json
	i = lr_eval_json("Buffer/File=store.json", 
	                 "JsonObject=f_json_obj", LAST);
	
	lr_json_stringify("JsonObject=f_json_obj",
	                  "OutputParam=d_result",
	                 LAST);
	
	// Multi match with SelectAll=No
	i = lr_json_find("JsonObject=f_json_obj",
	                "Value=fiction",
	                "QueryString=$.store.books[*].category",
	                 LAST);	
	if (i != 1)
		lr_error_message("lr_json_find should return %d, but returns %d", 1, i);
	
	// Multi match with SelectAll=Yes
	i = lr_json_find("JsonObject=f_json_obj",
	                "Value=fiction",
	                "QueryString=$.store.books[*].category",
	                "SelectAll=Yes",
	                 LAST);	
	if (i != 3)
		lr_error_message("lr_json_find should return %d, but returns %d", 1, i);
	
	// Use RegExp
	
	lr_save_string("$..books[?(@.price>10)].author", "f_path_author");
	
	i = lr_json_find("JsonObject=f_json_obj",
                "Value=.*Tolkien",
                "QueryString={f_path_author}",
                "UseRegExp=Yes",
                 LAST);
	
	if (i != 1)
		lr_error_message("lr_json_find should return %d, but returns %d", 1, i);
	
	return 0;
}

TestFind.c(10): 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}]}}".

TestFind.c(10): lr_json_stringify succeed

TestFind.c(15): Error: No matches were found.

TestFind.c(15): Error: lr_json_find failed