简体   繁体   English

MariaDB 从 JSON 文档中提取带有转义字符的值

[英]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.我正在使用 json_value 函数从传递给 Mariadb 数据库的 json 文档中提取值。 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当文档包含带有转义值的名称/值对时,例如 "Item_Name":"60\" Table",该函数仅提取 60 并为文档中的其余名称/值对返回空值。这是一个示例

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 ID
60 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.我也在 Mariadb 社区中发布了这个问题,并在那里得到了答案。 The trick is to add an additional escape character.诀窍是添加一个额外的转义字符。 Ian Gilfillan provided the answer in the Mariadb Community and his response is linked below. Ian Gilfillan 在 Mariadb 社区中提供了答案,他的回复链接如下。

https://mariadb.com/kb/en/extract-values-with-escaped-characters-from-json-document/+comments/5799#comment_5799 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. SQL 解析器将应用单个转义,但您希望将转义的字符串向下传递。 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    |
+-----------+------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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