简体   繁体   中英

MariaDB extract values with escaped characters from JSON Document

I am using the json_value function to extract values from a json document passed to a Mariadb database. When the document contains name/value pairs with escaped values such as "Item_Name":"60\" Table" the function only extracts 60 and returns null values for the rest of the name/value pairs in the document. Here is an example

Set @json = '{"Item_Name":"60\" Table", "Item_ID":"1"}';

select json_value(@json,'$.Item_Name') as Item_Name
, json_value(@json,'$.Item_ID') as ID

The results of this query is:

Item_Name ID
60 null

Not sure how to extract the value with the escaped character.

I posted this question in the Mariadb Community as well and recieved an answer there. The trick is to add an additional escape character. Ian Gilfillan provided the answer in the Mariadb Community and his response is linked below.

https://mariadb.com/kb/en/extract-values-with-escaped-characters-from-json-document/+comments/5799#comment_5799

Quote:

You need to use two escape characters. A single escape would be applied by the SQL parser, but you want to pass the escaped string down. So:

Set @json = '{"Item_Name":"60\\" Table", "Item_ID":"1"}';

select json_value(@json,'$.Item_Name') as Item_Name ,
json_value(@json,'$.Item_ID') as ID;
+-----------+------+
| Item_Name | ID   |
+-----------+------+
| 60" Table | 1    |
+-----------+------+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM