PostgreSQL 类型 json 的输入语法无效 输入字符串意外结束

ー P*_*e ー 5 sql postgresql json

当我尝试在 PostgreSQL pgadmin 上导入 JSON 文件时,我编写了以下脚本,但由于某种原因,它无法处理下面显示的错误。

\n

sql/plpgsql:

\n
DROP TABLE IF EXISTS temp01;\nDROP TABLE IF EXISTS temp_json;\ncreate temp table temp01 (\n    tmp text,\n    tmp02 text,\n    tmp03 text,\n    tmp04 text\n)\nwith (oids = false);\n\nBEGIN;\ncreate temporary table temp_json (values text) on commit drop;\ncopy temp_json from \'/home/yuis/pg/psql/tmp03.json\';\n\ninsert into temp01\nselect values->>\'id\' as tmp,\n       values->>\'created_at\' as tmp02,\n       values->>\'username\' as tmp03,\n       values->>\'tweet\' as tmp04\n    from (\n        select replace(values,\'\\\',\'\\\\\')::json as values from temp_json \n    )\nCOMMIT;\n\nSELECT * from temp01;\n
Run Code Online (Sandbox Code Playgroud)\n

上面的结果应该是这样的:

\n
tmp|tmp02|tmp03|tmp04 \n1396415271359897603,2021-05-23 19:38:39 JST,themooncarl,@elonmusk is still on our side.  t.co/K5DnByjzic\n1396414423057711109,2021-05-23 19:35:17 JST,..(and so on)\n
Run Code Online (Sandbox Code Playgroud)\n

错误:

\n
ERROR:  invalid input syntax for type json\nDETAIL:  The input string ended unexpectedly.\nCONTEXT:  JSON data, line 1: \nSQL state: 22P02\n
Run Code Online (Sandbox Code Playgroud)\n

JSON 文件“tmp03.json”:

\n
{"id": 1396415271359897603, "conversation_id": "1396415271359897603", "created_at": "2021-05-23 19:38:39 JST", "date": "2021-05-23", "time": "19:38:39", "timezone": "+0900", "user_id": 978732571738755072, "username": "themooncarl", "name": "The Moon ", "place": "", "tweet": "@elonmusk is still on our side.  t.co/K5DnByjzic", "language": "en", "mentions": [], "urls": [], "photos": ["https://pbs.twimg.com/media/E2EQSZgWQAELw9T.jpg"], "replies_count": 78, "retweets_count": 47, "likes_count": 570, "hashtags": [], "cashtags": [], "link": "https://twitter.com/TheMoonCarl/status/1396415271359897603", "retweet": false, "quote_url": "", "video": 1, "thumbnail": "https://pbs.twimg.com/media/E2EQSZgWQAELw9T.jpg", "near": "", "geo": "", "source": "", "user_rt_id": "", "user_rt": "", "retweet_id": "", "reply_to": [], "retweet_date": "", "translate": "", "trans_src": "", "trans_dest": ""}\n{"id": 1396414423057711109, "conversation_id": "1396414423057711109", "created_at": "2021-05-23 19:35:17 JST", "date": "2021-05-23", "time": "19:35:17", "timezone": "+0900", "user_id": 978732571738755072, "username": "themooncarl", "name": "The Moon ", "place": "", "tweet": "Me watching Bitcoin go down but realizing that it\xe2\x80\x99s just a nice opportunity to buy more for cheap.  t.co/GkmSEPmJCh", "language": "en", "mentions": [], "urls": [], "photos": ["https://pbs.twimg.com/media/E2EPg4ZXMAMIXjJ.jpg"], "replies_count": 94, "retweets_count": 34, "likes_count": 771, "hashtags": [], "cashtags": [], "link": "https://twitter.com/TheMoonCarl/status/1396414423057711109", "retweet": false, "quote_url": "", "video": 1, "thumbnail": "https://pbs.twimg.com/media/E2EPg4ZXMAMIXjJ.jpg", "near": "", "geo": "", "source": "", "user_rt_id": "", "user_rt": "", "retweet_id": "", "reply_to": [], "retweet_date": "", "translate": "", "trans_src": "", "trans_dest": ""}\n{"id": 1396388111840645120, "conversation_id": "1396388111840645120", "created_at": "2021-05-23 17:50:44 JST", "date": "2021-05-23", "time": "17:50:44", "timezone": "+0900", "user_id": 978732571738755072, "username": "themooncarl", "name": "The Moon ", "place": "", "tweet": "HODL!!! ", "language": "cs", "mentions": [], "urls": [], "photos": [], "replies_count": 263, "retweets_count": 149, "likes_count": 2299, "hashtags": [], "cashtags": [], "link": "https://twitter.com/TheMoonCarl/status/1396388111840645120", "retweet": false, "quote_url": "", "video": 0, "thumbnail": "", "near": "", "geo": "", "source": "", "user_rt_id": "", "user_rt": "", "retweet_id": "", "reply_to": [], "retweet_date": "", "translate": "", "trans_src": "", "trans_dest": ""}\n
Run Code Online (Sandbox Code Playgroud)\n

虽然上面显示“json 类型的输入语法无效”错误,但下面使用我在 SO 帖子上找到的更简单的示例 JSON,成功且没有语法错误。

\n
DROP TABLE IF EXISTS temp01;\nDROP TABLE IF EXISTS temp_json;\ncreate temp table temp01 (\n    tmp text,\n    tmp02 text,\n    tmp03 text,\n    tmp04 text\n)\nwith (oids = false);\n\nBEGIN;\ncreate temporary table temp_json (values text) on commit drop;\n-- copy temp_json from \'/home/yuis/pg/psql/tmp03.json\';\ncopy temp_json from \'/home/yuis/pg/psql/tmp.json\';\n\ninsert into temp01\n-- select values->>\'id\' as tmp,\n--        values->>\'created_at\' as tmp02,\n--     values->>\'username\' as tmp03,\n--     values->>\'tweet\' as tmp04\nselect values->>\'id\' as tmp,\n    values->>\'name\' as tmp02,\n    values->>\'comment\' as tmp03\n    from (\n        select replace(values,\'\\\',\'\\\\\')::json as values from temp_json\n    )\nCOMMIT;\n\nSELECT * from temp01;\n
Run Code Online (Sandbox Code Playgroud)\n

JSON,“tmp.json”:

\n
{"id": 23635,"name": "Jerry Green","comment": "Imported from facebook."}\n{"id": 23636,"name": "John Wayne","comment": "Imported from facebook."}\n
Run Code Online (Sandbox Code Playgroud)\n

https://yuis.xsrv.jp/images/ss/ShareX_ScreenShot_fa9740cb-905e-4c24-b763-7773bc9d1efe.png

\n

所以,显然这里的问题是来自 JSON 的语法错误,但是正如你所看到的 JSON 没有语法错误,显然问题出在 SQL 端。\n我不知道 JSON 和/或 SQL 中的哪里错误的。

\n

ー P*_*e ー 3

经过一番尝试,正如@jjanes提到的,我找到了这个问题的原因,这是因为json文件(tmp03.json)末尾有一个空行。

当我使用“cat > file”复制并粘贴到文件时,我无意中在行末尾按下了一个不必要的回车键,结果导致在 json 文件末尾创建一个空行。所以,这一行导致了错误。叹..

为了进一步理解这个问题,我做了一些额外的尝试。

  • tmp05.json,删除了最后一个“空”新行的 json 行

工作过

  • tmp03.json,最后一行有空行的json行(问题提到的错误)
ERROR:  invalid input syntax for type json
DETAIL:  The input string ended unexpectedly.
CONTEXT:  JSON data, line 1:
SQL state: 22P02
Run Code Online (Sandbox Code Playgroud)
  • tmp05_b.json,json行,但只有一行,没有新行

例如很喜欢

{"a": "aa"}{"b": "bb"}{"c": "cc"}
Run Code Online (Sandbox Code Playgroud)

而不是

{"a": "aa"}
{"b": "bb"}
{"c": "cc"}
Run Code Online (Sandbox Code Playgroud)
ERROR:  invalid input syntax for type json
DETAIL:  Expected end of input, but found "{".
CONTEXT:  JSON data, line 1: ...anslate": "", "trans_src": "", "trans_dest": ""}{...
SQL state: 22P02
Run Code Online (Sandbox Code Playgroud)
  • tmp05_c.json,json 行,但删除最后一个新行

删除了最后一个空行以及工作 tmp05.json,还删除了行尾的最后一个新行。

工作过