当前位置: 首页 > 工具软件 > JSON 2 HTML > 使用案例 >

mysql json invalid json text,Invalid JSON text in argument 2 - json_contains in MySQL 5.7.8

慕麒
2023-12-01

问题

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?

回答1:

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!

回答2:

If the column name store tags only (one level array), like ["ART","LIT","SPORTS"]

JSON_CONTAINS(column_name, 'ART', '$')

If the column name store an key-value array like {"tag":"ART","other":"NONE"}

JSON_CONTAINS(column_name, 'ART', '$.tag')

Finally if tag value is inside a parent array, you need to use path like this:

JSON_CONTAINS(column_name, 'ART', '$.parent.tag')

https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html#function_json-contains

回答3:

If your candidate type is string,

just add double quotes on your candidate then try again.

json_contains(\`column_name`,'"ART"')

JSON_CONTAINS(target, candidate[, path])

来源:https://stackoverflow.com/questions/33513625/invalid-json-text-in-argument-2-json-contains-in-mysql-5-7-8

 类似资料:

相关阅读

相关文章

相关问答