简体   繁体   English

插入到json列时出错,而文档在mysql 5.7中包含一个json_string作为值

[英]get a error while insert into json column while the document contains a json_string as value in mysql 5.7

always get a error when insert into json column when the json document contains a json string as value 当json文档包含json字符串作为值时插入到json列时总是会出错

msyql 5.7.25-0ubuntu0.18.04.2 msyql 5.7.25-0ubuntu0.18.04.2

sql with error: sql有错误:

INSERT INTO test VALUES(null, '{"a":"avalues","b":"{\"a\":\"avalues\"}"}');

but the sql below run well 但是下面的sql运行良好

INSERT INTO test VALUES(null, '{"a":"avalues","b":"{\'a\':\'avalues\'}"}');

You have to be careful escaping single quotes. 你必须小心逃避单引号。 But you do not need the \\" in the first query 但是在第一个查询中你不需要\\“

To insert a literal backslash in a string, you need to use \\\\ . 要在字符串中插入文字反斜杠,您需要使用\\\\

I got this to work on 5.7: 我让这个工作在5.7:

INSERT INTO test VALUES(null, '{"a":"avalues","b":"{\\"a\\":\\"avalues\\"}"}');

I queried the data and got this output: 我查询了数据并得到了这个输出:

select json_pretty(data) from test;
+----------------------------------------------------+
| json_pretty(data)                                  |
+----------------------------------------------------+
| {
  "a": "avalues",
  "b": "{\"a\":\"avalues\"}"
} |
+----------------------------------------------------+
1 row in set (0.01 sec)

I don't know why you are storing a JSON string inside a JSON document. 我不知道为什么要在JSON文档中存储JSON字符串。 That's too error-prone to be a good idea. 这太容易出错,不是个好主意。

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

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