【问题标题】:Postgres - escape double quotes inside escaped double quotesPostgres - 在转义双引号内转义双引号
【发布时间】:2020-10-19 05:07:07
【问题描述】:

我有一个名为 summary 的表,它有两列,idSERIALsummaryjson 数组 元素。

要插入此表,我必须遵循以下语法:

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


【解决方案1】:

通过在前面放置一个转义的反斜杠来双重转义双引号:

\"hello \\\"world\\\"\"

【讨论】:

    猜你喜欢
    • 2013-06-17
    • 2022-11-15
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    • 2020-11-26
    • 2015-11-06
    相关资源
    最近更新 更多