How to search JSON array in 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’, … Read more

How to update JSON data type column in MySQL 5.7.10?

Thanks @wchiquito for pointing me right direction. I solved the problem. Here is how I did it. mysql> select * from t1; +—————————————-+——+ | data | id | +—————————————-+——+ | {“key1”: “value1”, “key2”: “VALUE2”} | 1 | | {“key2”: “VALUE2”} | 2 | | {“key2”: “VALUE2”} | 1 | | {“a”: “x”, “b”: “y”, “key2”: … Read more

MySQL JSON: How to find object from key value

[*] MySQL 8.0 provides JSON_TABLE() to help with cases like this. select field_options.* from fields cross join json_table(fields.options, ‘$[*]’ columns( text text path ‘$.text’, value text path ‘$.value’ ) ) as field_options where field_options.value = 1; +——-+——-+ | text | value | +——-+——-+ | Grass | 1 | +——-+——-+ But you have to do this … Read more

How to search JSON data in MySQL?

If you have MySQL version >= 5.7, then you can try this: SELECT JSON_EXTRACT(name, “$.id”) AS name FROM table WHERE JSON_EXTRACT(name, “$.id”) > 3 Output: +——————————-+ | name | +——————————-+ | {“id”: “4”, “name”: “Betty”} | +——————————-+ Please check MySQL reference manual for more details: https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html