postgresql 将JSON文件导入Postgres 16数据库时出错(22P04在最后一个预期列之后有额外数据)

apeeds0o  于 2023-11-18  发布在  PostgreSQL
关注(0)|答案(1)|浏览(149)

所以我有下面的文件'14.json',我试图导入到postgres 16数据库的staging表中
现在我不能附加文件..但它的内容在这里:

14.json

{ "ChannelReadings": [
        { "ReadingsDto": [
                {
                    "Si": 47.67,
                    "Raw": 0,
                    "Conversion": 0,
                    "TimeStamp": "2023-01-24T12:57:43"
                },
                {
                    "Si": 47.22,
                    "Raw": 0,
                    "Conversion": 0,
                    "TimeStamp": "2023-01-24T13:02:43"
                },
                {
                    "Si": 47.6,
                    "Raw": 0,
                    "Conversion": 0,
                    "TimeStamp": "2023-01-24T13:07:43"
                },
                {
                    "Si": 47.5,
                    "Raw": 0,
                    "Conversion": 0,
                    "TimeStamp": "2023-01-24T13:12:43"
                }
            ],
            "ChannelId": 14
        },
        {
            "ReadingsDto": [
                {
                    "Si": 2.893605,
                    "Raw": 0,
                    "Conversion": 0,
                    "TimeStamp": "2023-01-24T13:07:43"
                }
            ],
            "ChannelId": 12
        },
        {
            "ReadingsDto": [
                {
                    "Si": 3.294233,
                    "Raw": 0,
                    "Conversion": 0,
                    "TimeStamp": "2023-01-24T13:07:43"
                }
            ],
            "ChannelId": 13
        },
        {
            "ReadingsDto": [
                {
                    "Si": 3.294233,
                    "Raw": 0,
                    "Conversion": 0,
                    "TimeStamp": "2023-01-24T13:07:43"
                }
            ],
            "ChannelId": 16
        }
    ],
    "DeviceSerialNumber": "894339",
    "RestartPointerNo": 5514732,
    "NewDownloadTable": false,
    "DataHashDto": "5Mckxoq42EeLHmLnimXv6A=="
}

字符串
我的代码如下:

DROP TABLE IF EXISTS tmp;

CREATE TEMP  table tmp (
      c TEXT
    );

COPY tmp FROM 'C:\ChrisDev\Readings\14.json';


但我得到以下错误:

[2023-11-16 12:44:32] [22P04] ERROR: extra data after last expected column
[2023-11-16 12:44:32] Where: COPY tmp, line 2: "        { "ReadingsDto": ["


我试着编辑这个没有用我在这里做错了什么?

64jmpszr

64jmpszr1#

您的文件中可能有制表符,这对copy...format text(默认行为)来说是不幸的,因为copy附带的另一个默认设置是delimiter配置为使用制表符:
FORMAT表示要读取或写入的数据格式:textcsv(逗号分隔值)或binary。默认值为text
DELIMITER指定文件每行(行)中分隔列的字符。默认值为text中的 * 制表符 *,CSV中的逗号。此字符必须是单个单字节字符。使用binary时不允许使用此选项。
要解决这个问题,请尝试另一种方法:

DROP TABLE IF EXISTS tmp;
CREATE TEMP  table tmp (c TEXT );
COPY tmp FROM 'C:\ChrisDev\Readings\14.json' WITH (FORMAT TEXT, DELIMITER '~');

字符串

相关问题