CONNECT JSON UDFs
Reference for the JSON user-defined functions bundled with the CONNECT engine: installing them, argument handling, and each Json, Jbin, and Jfile function.
Although such functions written by other parties do exist,[1] CONNECT provides its own UDFs that are specifically adapted to the JSON table type and easily available because, being inside the CONNECT library or DLL, they require no additional module to be loaded (see CONNECT - Compiling JSON UDFs in a Separate Library to make these functions in a separate library module).
Here is the list of the CONNECT functions; more can be added if required.
jbin_array
Function
STRING*
Make a JSON array containing its arguments.
jbin_array_add
Function
STRING*
Adds to its first array argument its second arguments.
jbin_array_add_values
Function
STRING*
Adds to its first array argument all following arguments.
jbin_array_delete
Function
STRING*
Deletes the nth element of its first array argument.
jbin_file
Function
STRING*
Returns of a (json) file contain.
jbin_get_item
Function
STRING*
Access and returns a json item by a JPATH key.
jbin_insert_item
Function
STRING
Insert item values located to paths.
jbin_item_merge
Function
STRING*
Merges two arrays or two objects.
jbin_object
Function
STRING*
Make a JSON object containing its arguments.
jbin_object_nonull
Function
STRING*
Make a JSON object containing its not null arguments.
jbin_object_add
Function
STRING*
Adds to its first object argument its second argument.
jbin_object_delete
Function
STRING*
Deletes the nth element of its first object argument.
jbin_object_key
Function
STRING*
Make a JSON object for key/value pairs.
jbin_object_list
Function
STRING*
Returns the list of object keys as an array.
jbin_set_item
Function
STRING
Set item values located to paths.
jbin_update_item
Function
STRING
Update item values located to paths.
jfile_bjson
Function
STRING
Convert a pretty=0 file to another BJson file.
jfile_convert
Function
STRING
Convert a Json file to another pretty=0 file.
jfile_make
Function
STRING
Make a json file from its json item first argument.
json_array
Function
STRING
Make a JSON array containing its arguments.
json_array_add
Function
STRING
Adds to its first array argument its second arguments.
json_array_add_values
Function
STRING
Adds to its first array argument all following arguments.
json_array_delete
Function
STRING
Deletes the nth element of its first array argument.
json_array_grp
Aggregate
STRING
Makes JSON arrays from coming argument.
json_file
Function
STRING
Returns the contains of (json) file.
json_get_item
Function
STRING
Access and returns a json item by a JPATH key.
json_insert_item
Function
STRING
Insert item values located to paths.
json_item_merge
Function
STRING
Merges two arrays or two objects.
json_locate_all
Function
STRING
Returns the JPATH’s of all occurrences of an element.
json_make_array
Function
STRING
Make a JSON array containing its arguments.
json_make_object
Function
STRING
Make a JSON object containing its arguments.
json_object
Function
STRING
Make a JSON object containing its arguments.
json_object_delete
Function
STRING
Deletes the nth element of its first object argument.
json_object_grp
Aggregate
STRING
Makes JSON objects from coming arguments.
json_object_list
Function
STRING
Returns the list of object keys as an array.
json_object_nonull
Function
STRING
Make a JSON object containing its not null arguments.
json_serialize
Function
STRING
Serializes the return of a “Jbin” function.
json_set_item
Function
STRING
Set item values located to paths.
json_update_item
Function
STRING
Update item values located to paths.
jsonvalue
Function
STRING
Make a JSON value from its unique argument.
jsoncontains
Function
INTEGER
Returns 0 or 1 if an element is contained in the document.
jsoncontains_path
Function
INTEGER
Returns 0 or 1 if a JPATH is contained in the document.
jsonget_string
Function
STRING
Access and returns a string element by a JPATH key.
jsonget_int
Function
INTEGER
Access and returns an integer element by a JPATH key.
jsonget_real
Function
REAL
Access and returns a real element by a JPATH key.
jsonlocate
Function
STRING
Returns the JPATH to access one element.
String values are mapped to JSON strings. These strings are automatically escaped to conform to the JSON syntax. The automatic escaping is bypassed when the value has an alias beginning with ‘json_’. This is automatically the case when a JSON UDF argument is another JSON UDF whose name begins with “json_” (not case sensitive). This is why all functions that do not return a Json item are not prefixed by “json_”.
Argument string values, for some functions, can alternatively be json file names. When this is ambiguous, alias them as jfile_. Full path should be used because UDF functions has no means to know what the current database is. Apparently, when the file name path is not full, it is based on the MariaDB data directory but I am not sure it is always true.
Numeric values are (big) integers, double floating point values or decimal values. Decimal values are character strings containing a numeric representation and are treated as strings. Floating point values contain a decimal point and/or an exponent. Integers are written without decimal points.
To install these functions execute the following commands:[2]
Note
Json function names are often written on this page with leading upper case letters for clarity. It is possible to do so in SQL queries because function names are case insensitive. However, when creating or dropping them, their names must match the case they are in the library module, which is in lower case.
On Unix systems (from Connect 1.7.02):
On Unix systems (from Connect 1.6):
On Unix systems (until Connect 1.5):
On WIndows (from Connect 1.7.02):
On WIndows (from Connect 1.6):
On WIndows (until Connect 1.5):
Jfile_Bjson
JFile_Bjson was introduced in MariaDB.
Converts the first argument pretty=0 json file to Bjson file. B(inary)json is a pre-parsed json format. It is described below in the Performance chapter (available in next Connect versions).
Jfile_Convert
JFile_Convert was introduced in MariaDB.
Converts the first argument json file to another pretty=0 json file. The third integer argument is the record length to use. This is often required to process huge json files that would be very slow if they were in pretty=2 format.
This is done without completely parsing the file, is very fast and requires no big memory.
Jfile_Make
Jfile_Make was added in CONNECT 1.4
The first argument must be a json item (if it is just a string, Jfile_Make will try its best to see if it is a json item or an input file name). The following arguments are a string file name and an integer pretty value (defaulting to 2) in any order. This function creates a json file containing the first argument item.
The returned string value is the created file name. If not specified as an argument, the file name can in some cases be retrieved from the first argument; in such cases the file itself is modified.
This function can be used to create or format a json file. For instance, supposing we want to format the file tb.json, this can be done with the query:
The tb.json file are changed to:
Json_Array_Add
Note: The following describes this function for CONNECT version 1.4 only. The first argument must be a JSON array. The second argument is added as member of this array:
[56,3.141600,"machin",null,"One more"]
Note: The first array is not escaped, its (alias) name beginning with ‘json_’.
Now we can see how adding an author to the JSAMPLE2 table can alternatively be done:
Note: Calling a column returning JSON a name prefixed by json_ (like json_author here) is good practice and removes the need to give it an alias to prevent escaping when used as an argument.
Additional arguments: If a third integer argument is given, it specifies the position (zero based) of the added value:
[5,3,4,8,7,9]
If a string argument is added, it specifies the Json path to the array to be modified. For instance:
{"a":1,"b":2,"c":[3,5,4]}
Json_Array_Add_Values
Json_Array_Add_Values added in CONNECT 1.4 replaces the function Json_Array_Add of CONNECT version 1.3.
The first argument must be a JSON array string. Then all other arguments are added as members of this array:
[56,3.141600,"machin",null,"One more","Two more"]
Json_Array_Delete
The first argument should be a JSON array. The second argument is an integer indicating the rank (0 based conforming to general json usage) of the element to delete:
[56,"foo",null]
Now we can see how to delete the second author from the JSAMPLE2 table:
A Json path can be specified as a third string argument
Json_Array_Grp
This is an aggregate function that makes an array filled from values coming from the rows retrieved by a query. Let us suppose we have the pet table:
John
dog
2
Bill
cat
1
Mary
dog
1
Mary
cat
1
Lisbeth
rabbit
2
Kevin
cat
2
Kevin
bird
6
Donald
dog
1
Donald
fish
3
The query:
will return:
Bill
Donald
John
Kevin
Lisbeth
Mary
One problem with the JSON aggregate functions is that they construct their result in memory and cannot know the needed amount of storage, not knowing the number of rows of the used table.
Therefore, the number of values for each group is limited. This limit is the value of JsonGrpSize whose default value is 10 but can be set using the JsonSet_Grp_Size function. Nevertheless, working on a larger table is possible, but only after setting JsonGrpSize to the ceiling of the number of rows per group for the table. Try not to set it to a very large value to avoid memory exhaustion.
JsonContains
This function can be used to check whether an item is contained in a document. Its arguments are the same than the ones of the JsonLocate function; only the return value changes. The integer returned value is 1 is the item is contained in the document or 0 otherwise.
JsonContains_Path
This function can be used to check whether a Json path is contained in the document. The integer returned value is 1 is the path is contained in the document or 0 otherwise.
Json_File
The first argument must be a file name. This function returns the text of the file that is supposed to be a json file. If only one argument is specified, the file text is returned without being parsed. Up to two additional arguments can be specified:
A string argument is the path to the sub-item to be returned. An integer argument specifies the pretty format value of the file.
This function is chiefly used to get the json item argument of other json functions from a json file. For instance, supposing the file tb.json is:
Extracting a value from it can be done with a query such as:
This query returns:
car
However, we’ll see that, most of the time, it is better to use Jbin_File or to directly specify the file name in queries. In particular this function should not be used for queries that must modify the json item because, even if the modified json is returned, the file itself would be unchanged.
Json_Get_Item
Json_Get_Item was added in CONNECT 1.4.
This function returns a subset of the json document passed as first argument. The second argument is the json path of the item to be returned and should be one returning a json item (terminated by a ‘*’). If not, the function will try to make it right but this is not foolproof. For instance:
The correct path should have been ‘second.*’), but in this simple case the function was able to make it right. The returned item:
["a",33]
Note: The array is aliased “json_second” to indicate it is a json item and avoid escaping it. However, the “json_” prefix is skipped when making the object and must not be added to the path.
JsonGet_Grp_Size
This function returns the JsonGrpSize value.
JsonGet_String / JsonGet_Int / JsonGet_Real
JsonGet_String, JsonGet_Int and JsonGet_Real were added in CONNECT 1.4.
The first argument should be a JSON item. If it is a string with no alias, it are converted as a json item. The second argument is the path of the item to be located in the first argument and returned, eventually converted according to the used function:
This query returns:
29.50
29
29.500000000000000
The function JsonGet_Real can be given a third argument to specify the number of decimal digits of the returned value. For instance:
This query returns:
29.50
The given path can specify all operators for arrays except the “expand” [*] operator). For instance:
The result:
89
5
45,28,36,45,89
243
48.60
Json_Item_Merge
This function merges two arrays or two objects. For arrays, this is done by adding to the first array all the values of the second array. For instance:
The function returns:
["a","b","c","d","e","f"]
For objects, the pairs of the second object are added to the first object if the key does not yet exist in it; otherwise the pair of the first object is set with the value of the matching pair of the second object. For instance:
The function returns:
{"a":1,"b":5,"c":3,"d":4,"f":6}
JsonLocate
The first argument must be a JSON tree. The second argument is the item to be located. The item to be located can be a constant or a json item. Constant values must be equal in type and value to be found. This is "shallow equality" – strings, integers and doubles won't match.
This function returns the json path to the located item or null if it is not found:
This query returns:
$.AUTHORS[1].FN
The path syntax is the same used in JSON CONNECT tables.
By default, the path of the first occurrence of the item is returned. The third parameter can be used to specify the occurrence whose path is to be returned. For instance:
$[0]
$[2][1]
$[2]
For string items, the comparison is case sensitive by default. However, it is possible to specify a string to be compared case insensitively by giving it an alias beginning by “ci”:
$.AUTHORS[0].LN
Json_Locate_All
The first argument must be a JSON item. The second argument is the item to be located. This function returns the paths to all locations of the item as an array of strings:
This query returns:
["$[0][0]","$[1][0][1]"]
The returned array can be applied other functions. For instance, to get the number of occurrences of an item in a json tree, you can do:
The displayed result:
2
If specified, the third integer argument set the depth to search in the document. This means the maximum items in the paths. This value defaults to 10 but can be increased for complex documents or reduced to set the maximum wanted depth of the returned paths.
Json_Make_Array
Json_Make_Array returns a string denoting a JSON array with all its arguments as members:
[56,3.141600,"My name is "Foo"",null]
Note: The argument list can be void. If so, a void array is returned.
Json_Make_Object
Json_Make_Object returns a string denoting a JSON object. For instance:
The object is filled with pairs corresponding to the given arguments. The key of each pair is made from the argument (default or specified) alias.
{"56":56,"3.1416":3.141600,"machin":"machin","NULL":null}
When needed, it is possible to specify the keys by giving an alias to the arguments:
{"qty":56,"price":3.141600,"truc":"machin","garanty":null}
If the alias is prefixed by ‘json_’ (to prevent escaping) the key name is stripped from that prefix.
This function is chiefly useful when entering values retrieved from a table, the key being by default the column name:
{"matricule":40567,"nom":"PANTIER","titre":"DIRECTEUR","salaire":14000.000000}
Json_Object_Add
The first argument must be a JSON object. The second argument is added as a pair to this object:
{"item":"T-shirt","qty":27,"price":24.990000,"color":"blue"}
Note: If the specified key already exists in the object, its value is replaced by the new one.
The third string argument is a Json path to the target object.
Json_Object_Delete
The first argument must be a JSON object. The second argument is the key of the pair to delete:
{"item":"T-shirt","price":24.99}
The third string argument is a Json path to the object to be the target of deletion.
Json_Object_Grp
This function works like Json_Array_Grp. It makes a JSON object filled with value pairs whose keys are passed from its first argument and values are passed from its second argument.
This can be seen with the query:
This query returns:
Bill
{"cat":1}
Donald
{"dog":1,"fish":3}
John
{"dog":2}
Kevin
{"cat":2,"bird":6}
Lisbeth
{"rabbit":2}
Mary
{"dog":1,"cat":1}
Json_Object_Key
Return a string denoting a JSON object. For instance:
The object is filled with pairs made from each key/value arguments.
{"qty":56,"price":3.141600,"truc":"machin","garanty":null}
Json_Object_List
The first argument must be a JSON object. This function returns an array containing the list of all keys existing in the object:
["qty","price","truc","garanty"]
Json_Object_Nonull
This function works like Json_Make_Object but “null” arguments are ignored and not inserted in the object. Arguments are regarded as “null” if they are JSON null values, void arrays or objects, or arrays or objects containing only null members.
It is mainly used to avoid constructing useless null items when converting tables (see later).
Json_Object_Values
The first argument must be a JSON object. This function returns an array containing the list of all values existing in the object:
[1,2,3]
JsonSet_Grp_Size
This function is used to set the JsonGrpSize value. This value is used by the following aggregate functions as a ceiling value of the number of items in each group. It returns the JsonGrpSize value that can be its default value when passed 0 as argument.
Json_Set_Item / Json_Insert_Item / Json_Update_Item
These functions insert or update data in a JSON document and return the result. The value/path pairs are evaluated left to right. The document produced by evaluating one pair becomes the new value against which the next pair is evaluated.
Json_Set_Item replaces existing values and adds non-existing values.
Json_Insert_Item inserts values without replacing existing values.
Json_Update_Item replaces only existing values.
Example:
This query returns:
[1,"foo",3,{"quatre":4,"cinq":5}]
[1,2,3,{"quatre":4,"cinq":5}]
[1,"foo",3,{"quatre":4}]
JsonValue
Returns a JSON value as a string, for instance:
3.141600
Notes
See for instance: json-functions, lib_mysqludf_json#readme and json_udf_functions_version_04
This will not work when CONNECT is compiled embedded
This page is licensed: CC BY-SA / Gnu FDL
Last updated
Was this helpful?

