我正在尝试从公共 APE 获取一些 JSON 数据并将记录插入到我的 SQL Azure 数据库中。API 请求正在工作,JSON 节点中的转换为对象也是如此,但我无法缝合以获取OPENJSON()正确解析 json 数据对象的函数。我收到此错误:
RequestError:JSON 文本格式不正确。在位置 1 发现了意外的字符“o”。
从api获取的json数据为:
{
"data":[{
"from": "2021-09-22T23:00Z",
"to": "2021-09-22T23:30Z",
"intensity": {
"forecast": 105,
"actual": 91,
"index": "low"
}
},
{
"from": "2021-09-22T23:30Z",
"to": "2021-09-23T00:00Z",
"intensity": {
"forecast": 92,
"actual": 80,
"index": "low"
}
},
{
"from": "2021-09-23T22:00Z",
"to": "2021-09-23T22:30Z",
"intensity": {
"forecast": 153,
"actual": null,
"index": "low"
}
},
{
"from": "2021-09-23T22:30Z",
"to": "2021-09-23T23:00Z",
"intensity": {
"forecast": 150,
"actual": null,
"index": "low"
}
}]
}
SQL Azure 节点具有以下代码:
DECLARE @data NVARCHAR(MAX) = N'{{{payload}}}';
INSERT INTO TCarbonIntensity ([from], [to], [forecast], [actual], [index])
SELECT [from], [to], [forecast], [actual], [index]
FROM OPENJSON(@data)
WITH (
[from] datetime '$.from',
[to] datetime '$.to',
[forecast] int '$.intensity.forecast',
[actual] int '$.intensity.actual',
[index] nvarchar(5) '$.intensity.index'
);