How to search JSON array in MySQL?

MysqlMysql Json

Mysql Problem Overview


Let's say I have a JSON column named data in some MySQL table, and this column is a single array. So, for example, data may contain:

> [1,2,3,4,5]

Now I want to select all rows which have a data column where one of its array elements is greater than 2. Is this possible?

I tried the following, but seems it is always true regardless of the values in the array:

SELECT * from my_table
WHERE JSON_EXTRACT(data, '$[*]') > 2;

Mysql Solutions


Solution 1 - Mysql

You may search an array of integers as follows:

  JSON_CONTAINS('[1,2,3,4,5]','7','$') Returns: 0
  JSON_CONTAINS('[1,2,3,4,5]','1','$') Returns: 1

You may search an array of strings as follows:

  JSON_CONTAINS('["a","2","c","4","x"]','"x"','$') Returns: 1
  JSON_CONTAINS('["1","2","3","4","5"]','"7"','$') Returns: 0

Note: JSON_CONTAINS returns either 1 or 0

In your case you may search using a query like so:

SELECT * from my_table
WHERE JSON_CONTAINS(data, '2', '$');

Solution 2 - Mysql

SELECT JSON_SEARCH('["1","2","3","4","5"]', 'one', "2") is not null 

is true

SELECT JSON_SEARCH('["1","2","3","4","5"]', 'one', "6") is not null

is false

Solution 3 - Mysql

Since MySQL 8 there is a new function called JSON_TABLE.

CREATE TABLE my_table (id INT, data JSON);

INSERT INTO my_table VALUES 
  (1, "[1,2,3,4,5]"), 
  (2, "[0,1,2]"), 
  (3, "[3,4,-10]"), 
  (4, "[-1,-2,0]");

SELECT DISTINCT my_table.* 
FROM my_table, JSON_TABLE(data, "$[*]" COLUMNS(nr INT PATH '$')) as ids 
WHERE ids.nr > 2;

+------+-----------------+
| id   | data            |
+------+-----------------+
|    1 | [1, 2, 3, 4, 5] |
|    3 | [3, 4, -10]     |
+------+-----------------+
2 rows in set (0.00 sec)

Solution 4 - Mysql

I use a combination of JSON_EXTRACT and JSON_CONTAINS (MariaDB):

SELECT * FROM table WHERE JSON_CONTAINS(JSON_EXTRACT(json_field, '$[*].id'), 11, '$');

Solution 5 - Mysql

I don't know if we found the solution. I found with MariaDB a way, to search path in a array. For example, in array [{"id":1}, {"id":2}], I want find path with id equal to 2.

SELECT JSON_SEARCH('name_field', 'one', 2, null, '$[*].id')
FROM name_table

The result is:

"$[1].id"

The asterisk indicate searching the entire array

Solution 6 - Mysql

A possible way is to deal with the problem as string matching. Convert the JSON to string and match.

Or you can use JSON_CONTAINS.

Solution 7 - Mysql

This example works for me with mysql 5.7 above

SET @j = '{"a": [ "8428341ffffffff", "8428343ffffffff", "8428345ffffffff", "8428347ffffffff","8428349ffffffff", "842834bffffffff", "842834dffffffff"], "b": 2, "c": {"d": 4}}';
select JSON_CONTAINS(JSON_EXTRACT(@j , '$.a'),'"8428341ffffffff"','$') => returns 1
             

notice about " around search keyword, '"8428341ffffffff"'

Solution 8 - Mysql

You can use JSON extract to search and select data

SELECT data, data->"$.id" as selectdata
FROM table
WHERE JSON_EXTRACT(data, "$.id") = '123'
#ORDER BY c->"$.name";
limit 10 ;

Solution 9 - Mysql

SET @doc = '[{"SongLabels": [{"SongLabelId": "111", "SongLabelName": "Funk"}, {"SongLabelId": "222", "SongLabelName": "RnB"}], "SongLabelCategoryId": "test11", "SongLabelCategoryName": "曲风"}]';
SELECT *,  JSON_SEARCH(@doc, 'one', '%un%', null, '$[*].SongLabels[*].SongLabelName')FROM t_music_song_label_relation;

result: "$[0].SongLabels[0].SongLabelName"

SELECT song_label_content->'$[*].SongLabels[*].SongLabelName' FROM t_music_song_label_relation;

result: ["Funk", "RnB"]

Solution 10 - Mysql

I have similar problem, search via function

create function searchGT(threshold int, d JSON)
returns int
    begin
        set @i = 0;
        while @i < json_length(d) do
            if json_extract(d, CONCAT('$[', @i, ']')) > threshold then
                return json_extract(d, CONCAT('$[', @i, ']'));
            end if;
            set @i = @i + 1;
        end while;
        return null;
    end;

select searchGT(3, CAST('[1,10,20]' AS JSON));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1011792View Question on Stackoverflow
Solution 1 - MysqlMNU-548View Answer on Stackoverflow
Solution 2 - MysqlJONG MIN IMView Answer on Stackoverflow
Solution 3 - Mysqluser2458995View Answer on Stackoverflow
Solution 4 - MysqlRodolfo Jorge Nemer NogueiraView Answer on Stackoverflow
Solution 5 - MysqlCarlos 2VView Answer on Stackoverflow
Solution 6 - MysqlMohamed Gad-ElrabView Answer on Stackoverflow
Solution 7 - MysqlMajid HojatiView Answer on Stackoverflow
Solution 8 - MysqlJustin JView Answer on Stackoverflow
Solution 9 - MysqlTom HuView Answer on Stackoverflow
Solution 10 - MysqlzoolView Answer on Stackoverflow