MySQL JSON_SEARCH() Function
In MySQL, the JSON_SEARCH() function returns the path of a given string in a JSON document.
JSON_SEARCH() Syntax
Here is the syntax of the MySQL JSON_SEARCH() function:
JSON_SEARCH(json, one_or_all, search_str)
JSON_SEARCH(json, one_or_all, search_str, escape_char)
JSON_SEARCH(json, one_or_all, search_str, escape_char, path)
Parameters
-
jsonRequired. A JSON document.
-
one_or_allRequired. Available values:
'one'and'all'. This parameter effects the search as follows:- If it is
'one', theJSON_SEARCH()function will return the first matching path. - If it is
'all', theJSON_SEARCH()function will return all matching paths. All paths will be wrapped in an array and returned.
- If it is
-
search_strRequired. The string to search for. You can use
%and_wildcards in the parameter, just likeLIKE:%matches any number of any characters._matches an arbitrary character.
-
escape_charOptional. If
search_strcontains%or_, you need to add a escape character before them. The default is\. -
pathOptional. Only perform a search in the part specified by the path.
Return value
The JSON_SEARCH() function returns a path string or an array including all matching path.
This parameter one_or_all effects the search as follows:
- If it is
'one', theJSON_SEARCH()function will return the first matching path. - If it is
'all', theJSON_SEARCH()function will return all matching paths. All paths will be wrapped in an array and returned.
The JSON_SEARCH() function will return NULL in the following cases:
- The specified string was not found
- The specified
pathdoes not exist in the JSON document - Any parameter is
NULL
There will happen an error in the following cases:
- MySQL will give an error if the parameter
jsonis not a valid JSON document. You can useJSON_VALID()to verify the JSON document. - If the parameter
pathis not a valid path expression, MySQL will give an error.
JSON_SEARCH() Examples
First, Let’s create a JSON document for the following examples:
SET @json = '[
{
"name": "Tim",
"age": 20,
"hobbies": [
{ "name": "Car", "weight": 10 },
{ "name": "Sports", "weight": 20 }
]
},
{
"name": "Tom",
"age": 20,
"hobbies": [
{ "name": "Reading", "weight": 10 },
{ "name": "Sports", "weight": 20 }
]
}
]';
Here, we create a JSON array that contains two user information.
Seach
SELECT JSON_SEARCH(@json, 'one', 'Tim');
+----------------------------------+
| JSON_SEARCH(@json, 'one', 'Tim') |
+----------------------------------+
| "$[0].name" |
+----------------------------------+one vs all
SELECT
JSON_SEARCH(@json, 'one', 'Sports'),
JSON_SEARCH(@json, 'all', 'Sports');
+-------------------------------------+--------------------------------------------------+
| JSON_SEARCH(@json, 'one', 'Sports') | JSON_SEARCH(@json, 'all', 'Sports') |
+-------------------------------------+--------------------------------------------------+
| "$[0].hobbies[1].name" | ["$[0].hobbies[1].name", "$[1].hobbies[1].name"] |
+-------------------------------------+--------------------------------------------------+Using wildcards
SELECT JSON_SEARCH(@json, 'all', 'S%');
+--------------------------------------------------+
| JSON_SEARCH(@json, 'all', 'S%') |
+--------------------------------------------------+
| ["$[0].hobbies[1].name", "$[1].hobbies[1].name"] |
+--------------------------------------------------+Here, we searched for a string with wildcard S% that means there can be any characters after S.