提问人:ChrisAsi71 提问时间:11/17/2023 更新时间:11/18/2023 访问量:40
22P02 错误:json 类型 -> 在 Postgres 中访问 JSON 的输入语法无效 [已关闭]
22P02 ERROR: invalid input syntax for type json -> Accessing JSON in Postgres [closed]
问:
我正在尝试查询JSON文件以提取字段并遇到问题。
这是我引用的 JSON 文件 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' WITH (FORMAT TEXT, DELIMITER '~');
当我运行以下代码时:
select
c::json ->> 'DeviceSerialNumber' as SerialNumber,
(c::json ->> 'RestartPointerNo')::int as RestartPointerNo
from tmp
我收到以下错误:
[2023-11-16 23:42:08] [22P02] ERROR: invalid input syntax for type json
[2023-11-16 23:42:08] Detail: The input string ended unexpectedly.
[2023-11-16 23:42:08] Where: JSON data, line 1: {"ChannelReadings": [
问题我需要在此处更改哪些内容才能提取 DeviceSerialNumber 和 RestartPointerNo 的值
答:
-2赞
ChrisAsi71
11/17/2023
#1
因此,按照 Nick 给出的建议,我从输入文件中删除了所有 CR LF 代码,它奏效了!
评论
1赞
Kennedy Nyaga
11/17/2023
请将其添加为评论而不是答案。
评论
COPY tmp FROM