【问题标题】:Postgresql copy fails on pretty printed JSONPostgresql 复制在漂亮打印的 JSON 上失败
【发布时间】:2016-02-10 23:29:21
【问题描述】:

我有一个包含 JSON 数据的文件。该文件是“漂亮打印”给我的(带有缩进等),但 PostgreSQL \copy 无法加载它。它给出了以下错误:

CONTEXT:  JSON data, line 1: {
COPY test_data, line 1, column data: "{"

这个错误很容易重现。创建临时表:

 CREATE TEMP TABLE target(data jsonb); 

接下来创建 2 个文件。在 file1 中,输入以下文本:

{
    "catalog_name":"foo","sale_date":"2016-02-09T00:00:00"
}

在文件 2 中:

{"catalog_name":"foo","sale_date":"2016-02-09T00:00:00"}

\copy 在 file1 上失败,但在 file2 上成功。

根据jsonlint.com,它们都是有效的 json 字符串。

是否可以在不修改文件的情况下使用\copy 加载file2?

我使用的是 PostgreSQL 9.4.5

【问题讨论】:

标签: json postgresql psql postgresql-9.4


【解决方案1】:

如果您的数据对换行不是很敏感,您可以尝试从JSON 输入中去除它们。

例如

cat input.json | tr -d '\n' > input2.json

使用新的输入文件应该可以工作。

【讨论】:

    猜你喜欢
    • 2017-05-01
    • 2013-11-08
    • 2014-05-19
    • 2018-05-27
    • 1970-01-01
    • 2021-01-31
    • 2014-05-12
    • 2021-12-26
    相关资源
    最近更新 更多