Extract values with escaped characters from JSON Document

You are viewing an old version of this question. View the current version here.

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_NameID
:----------:----
60null

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

Comments

Comments loading...
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.