I’m storing JSON data in JSON type column in MySQL database. Tried to filter extra_data
column for where payment_address
is not null
. Unfortunately couldn’t find the right way.
These are the queries I tried:
SELECT * FROM orders WHERE extra_data->"$.payment_address" != NULL;
SELECT * FROM orders WHERE extra_data->"$.payment_address" IS NOT NULL;
SELECT * FROM orders WHERE extra_data->"$.payment_address" != "null";
For the extra_data
column, JSON data is like this:
extra_data |
---|
{"tracking_number": "", "payment_amount": null, "payment_address": null} |
{"tracking_number": "", "payment_amount": null, "payment_address": "testaddress"} |
How can I query some field in JSON column for null/not null values?
2
Answers
You could use
REPLACE
to replace ‘null’ value into empty value, then get only not empty values :Result :
Demo here
This is likely related to bug 85755 which relates to the difference between JSON
null
and SQLNULL
. Anyway, you can workaround this by using->>
orJSON_UNQUOTE
instead:In both cases the output is:
Demo on db-fiddle