Warm tip: This article is reproduced from serverfault.com, please click

MySQL query from JSON datatype column that has backslash character

发布于 2020-11-28 01:28:37

I have a difficulty to query element in JSON column in MySQL

This is my data : enter image description here

example of whats in the column : {\order\":[{\"product_id\":3,\"quantity\":5,\"product_name\":\"Ikan salmon bakar \"}]}"

I'm using this sql syntax :

SELECT JSON_EXTRACT(order_list, '$.order[*].product_id') FROM orders_copy WHERE id = 3;

If I use this query to the column that contain backslash, then it will produce null.

enter image description here

Expected result (eg:) :

enter image description here

I'm not sure if I should get rid of the backslash so that the query will produce result or is there any effective way solving it ?.

Thanks a lot

Questioner
Syahirah AR
Viewed
0
Syahirah AR 2020-11-29 14:39:43

I found the answers for problem I stated above.

It is just that I found the answer through this link : https://dev.mysql.com/doc/refman/5.7/en/json-modification-functions.html#function_json-replace , where I search for what are the code that are compatible with MySQL version to cater special character. For backslash, I'm using JSON_UNQUOTE()

Here I put the solution : https://www.db-fiddle.com/f/i7EpX4Qq86iJWFkPgzMhVX/0

Thanks to those who help:)