Postgres INSERT 为 json 返回“无效的输入语法”

Posted

技术标签:

【中文标题】Postgres INSERT 为 json 返回“无效的输入语法”【英文标题】:Postgres INSERT returning 'invalid input syntax' for json 【发布时间】:2021-12-31 05:06:35 【问题描述】:

问题:尝试将 JSON 字符串插入 json 数据类型的 Postgres 表列会间歇性地在某些记录插入尝试中返回此错误,但在其他尝试中不返回此错误。

我使用多个第三方“JSON 验证器”应用程序确认我插入的 JSON 确实有效,并且我已经确认任何单个 ' 引号字符都已使用双 '' 技术转义,并且问题持续存在。

还有哪些需要考虑的其他故障排除步骤?

这是我尝试过的一个清理过的 JSON 示例:

"id": "jf4ba72kFNQ","publishedAt": "2012-09-02T06:07:28Z","channelId": "UCrbUQCaozffv1soNdfDROXQ","title": "Scout vs. Witch: a tale of boy meets ghoul (Official Version)","tags": ["L4D","TF2","SFM","animation","zombies","Valve","video game"],"description": "Howdy folks (he''s alive!). I made a new SFM video (October 2015), called \"Nick in a Hotel Room\". Please check it out: https://www.youtube.com/watch?v=FOCTgwBIun0\n\nAlso check out some early behind the scenes of Scout vs. Witch:\nhttps://www.youtube.com/watch?v=73tQEBgD09I\n\nYou can find links to my stuff on my website: http://nailbiter.net\n\n-----\n\nhey gang,\nI''m the animator who made this cartoon. Hope you like it.\n\nThis is my little mash-up of a bunch of stuff I like. What happens when the Scout from Valve''s Team Fortress 2 video-game walks into the wrong neighborhood (Left 4 Dead). Hilarity (and a bodycount) ensues. It was created using Source Film Maker (for all the dialog stuff and the montage at the beginning), and with TF2/Source SDK for the entire 300 alley-run sequence. I had already completed that part before SFM was released. The big zombie horde scenes and a couple others were shot in Left 4 Dead. I hope you get a kick out of it.\n\nStuff I did:\nI animated all of the characters (using Maya) except for the big crowd scenes and parts of the headcrab zombie (the crawling and the legs). The faces in the dialog scenes were animated in SFM.\n\nAlso did additional mapping, particles, motion graphics, zombie maya rigging, and created blendshapes for the Witch''s face to enable her to talk/emote. I didn''t do a full set, just the phonemes I needed for this performance. Inspiration for her performance was based on Meg Mucklebones (if you''ve ever seen Legend) mixed with the demon ladies in Army of Darkness. I have a feeling Valve had seen those movies too when they designed her..\n\nthanks for watching."

【问题讨论】:

可以发个例子吗? @Jellyboy 谢谢,我分享了一个例子。另外,我回答了这个问题,这是我一直在寻找的东西,但找不到任何连贯的答案来回答我的问题(至少,不是以我询问或搜索的方式),所以我自己写了!跨度> 你的例子对我有用。如果你擦洗的是错误的原因,那就是违背了目的。错误消息的文本通常会提供有关问题所在和位置的良好信息。但是json越大,越难根据消息找到错误。 【参考方案1】:

我通过列举到目前为止我发现的所有其他故障排除步骤来回答这个问题,要么是“现场工作人员”将拥有的“工作知识”,要么是更晦涩的(或埋在 postgres 文档中,虽然彻底,是深奥的)我通过自己的试验和错误发现的见解

步骤

    确保您已通过使用 '' 进行双转义来转义任何单引号 ' 字符 确保您的 JSON 字符串实际上是单行字符串 - JSON 很容易复制为多行字符串,而 postgres JSON 列不会接受这一点(就像在任何换行符上按退格键一样简单) 我发现的最晦涩难懂的地方:即使封装在 JSON 字符串字段中,? 问号也很奇怪地破坏了 postgres 的 JSON 语法。 "url": "myurl.com?queryParam=someId" 之类的东西将返回无效。通过转义问号来解决这个问题:"url": "myurl.com\?queryParam=someId"

【讨论】:

? 不需要转义。 dbfiddle.uk/… - 可能是你的代码插入了值。 也从未遇到过多行内容的问题 您的示例是有效的 JSON。 select '...paste here...'::jsonb; 工作正常。可能字符串在发送到 Postrgres 之前 被扭曲了? @FrankHeikens:当尝试多行值dbfiddle.uk/… 时,我得到“* 值为 0x0a 的字符必须被转义。*”

以上是关于Postgres INSERT 为 json 返回“无效的输入语法”的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Postgres 中选择一列 json 对象,以便返回的行是 json 数组?

在 SQL (Postgres) 中作为 JSON 对象数组返回

从 Postgres json 中的数组返回一系列元素

postgres 中的条件 INSERT INTO 语句

使用 Postgres 11 时,UPDATE 中不允许设置返回函数

Postgres 检查空 JSONB 字段