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.
Answer
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 |
+-----------+------+