I'm using MySQL 8.0 (inside Azure Database for MySQL Server).
I've noticed some strange behavior while querying with the "json_extract" functionality. In the example below, I am querying a VARCHAR field that contains the valid JSON text: {"myfield":"123456"}.
SELECT json_extract(jsontext, '$.myfield') raw_extract,
LENGTH(json_extract(jsontext, '$.myfield')) raw_extract_length,
CASE WHEN json_extract(jsontext, '$.myfield') = '123456' THEN 'true' ELSE 'false' END matches_no_quotes,
CASE WHEN json_extract(jsontext, '$.myfield') = '"123456"' THEN 'true' ELSE 'false' END matches_with_quotes,
CASE WHEN json_extract(jsontext, '$.myfield') LIKE '"%"' THEN 'true' ELSE 'false' END matches_wildcard_dbl_quotes
What is odd is that the value returned by json_extract appear to behave differently depending on what comparison is interacting with it:
- raw_extract -> "123456"
- raw_extract_length -> 8
- matches_no_quotes -> true
- matches_with_quotes -> false
- matches_wildcard_dbl_quotes -> true
As you can see, there's basically two return values: one with the double-quotes included and one without. LENGTH(), the immediate return value, and the LIKE operators act as though the double-quotes exist. Equality (=) does not act as though the quotes exist.
My best theory is that the direct return type is not VARCHAR and is instead some sort of JSON datatype, but the MySQL documentation didn't confirm or deny that. As someone encountering this for the first time, this contradictory behavior is very confusing. Can anyone shed some light on it?
CASE WHEN json_extract(jsontext, '$.myfield') = 123456 THEN 'true' ELSE 'false' END
returns false.