Invalid JSON text in argument 2 - json_contains in MySQL 5.7.8

Alex Beals picture Alex Beals · Nov 4, 2015 · Viewed 21.3k times · Source

I have a database with one column that is JSON of strings (ex. ["ART","LIT"], etc.). I want to search it using json_contains.

However, when I try:

json_contains(\`column_name`,"ART")

It errors saying:

Invalid JSON text in argument 2 to function json_contains: "Invalid value." at position 0 in 'ART'.

Note that json_contains doesn't error with numbers in the place of "ART", just with strings. Any idea what I can do to fix/get around this?

Answer

Alex Beals picture Alex Beals · Nov 4, 2015

Apparently, it treats integers differently from strings. While json_contains(`column_name`,"1") is a valid call, to check if it contains "ART", you must use json_contains(`column_name`,'"ART"').

That resolved my issue!