【发布时间】:2020-10-19 05:07:07
【问题描述】:
我有一个名为 summary 的表,它有两列,id 是 SERIAL 和 summary 是json 数组 元素。
要插入此表,我必须遵循以下语法:
insert into summary values (default, '{"{\"index\": -1, \"nivel\": -1, `\"title\": \"hello world\"`, \"children\": []}"}')
但如果我想在标题中转义双引号,例如:
insert into summary values (default, '{"{\"index\": -1, \"nivel\": -1, \"title\": \"hello \"world\"\", \"children\": []}"}')
它返回这个错误:
ERROR: invalid input syntax for type json
LINE 1: insert into summary values (default, '{"{\"index\": -1, \"ni...
^
DETAIL: Token "world" is invalid.
CONTEXT: JSON data, line 1: {"index": -1, "nivel": -1, "title": "hello "world...
我怎样才能正确地逃脱?
【问题讨论】:
-
您可以删除默认设置并尝试像这样
insert into summary(symmary_col) values ( '{"{\"index\": -1, \"nivel\": -1,\"title\": \"hello world\", \"children\": []}"}')。你不必指定默认值,postgres 会处理它。 -
为什么是
json[]而不是json(或更好:jsonb)并使用“json 数组”`? -
考虑插入一个 postgres 数组并让 postgres 处理存储它
标签: sql json postgresql