MySQL select where JSON field property has value

Some examples of how to query a json data type field:

SELECT * FROM users WHERE JSON_EXTRACT(meta_data, "$.first_name") = 'bob';

SELECT * FROM users WHERE JSON_EXTRACT(meta_data, "$.age") IS NOT NULL;

SELECT * FROM users WHERE JSON_EXTRACT(meta_data, "$.accepted_policy") = true;

With mysql 5.7.9 +

You can also just do this (shortcut for JSON_EXTRACT):

SELECT * FROM users WHERE meta_data->"$.first_name" = 'bob'

You might notice your json data results are "quoted". You could use JSON_UNQUOTE, or you could use this, which is a shortcut of JSON_EXTRACT & JSON_UNQUOTE:

SELECT meta_data->>"$.first_name" FROM users WHERE meta_data->>"$.first_name" IS NOT NULL

And to select data from within sub objects:

SELECT meta_data->>"$.address.tel" FROM users WHERE meta_data->>"$.address.street" = "123 Main St"

docs: https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html


Use Below query for WHERE operation with JSON Datatype Field in mysql

SELECT meta_data->'$.first_name' meta_data FROM users WHERE INSTR(meta_data->'$.first_name','123') > 0