我正在尝试将 Azure 表单识别的输出映射到 SQL 表,但很难分解 JSON 输出,这是我尝试执行的 SQL 语句
declare @RJSON NVARCHAR(MAX) = N'{
"status": "Succeeded",
"understandingResults": [
{
"pages": [
1
],
"fields": {
"Subtotal": null,
"Total": {
"valueType": "numberValue",
"value": 52.54,
"text": "$52,54",
"elements": [
{
"$ref": "#/recognitionResults/0/lines/82/words/0"
}
]
},
"Tax": null,
"MerchantAddress": null,
"MerchantName": {
"valueType": "stringValue",
"value": "OP BRITISH CHICKEN CP BRITISH CHICKEN",
"text": "OP BRITISH CHICKEN CP BRITISH CHICKEN",
"elements": [
{
"$ref": "#/recognitionResults/0/lines/27/words/0"
},
{
"$ref": "#/recognitionResults/0/lines/27/words/1"
},
{
"$ref": "#/recognitionResults/0/lines/27/words/2"
},
{
"$ref": "#/recognitionResults/0/lines/29/words/0"
},
{
"$ref": "#/recognitionResults/0/lines/29/words/1"
},
{
"$ref": "#/recognitionResults/0/lines/29/words/2"
}
]
},
"MerchantPhoneNumber": null,
"TransactionDate": null,
"TransactionTime": null
}
}
]
}'
SELECT
OJC.value
,JSON_VALUE(OJC.[value],'$.text') as text
FROM OPENJSON(@RJSON) OJC
CROSS APPLY OPENJSON(JSON_QUERY(OJC.[value]),'$.fields.Total') OCV
where
OJC.[value]<>'Succeeded'
我尝试过各种组合,但没有任何乐趣。 看起来 JSON 的格式化方式无法遍历嵌套数组。
最佳答案
终于解决了:)
SELECT
TotalValue=c.[value]
,Merchant=d.[value]
,ExpenseDate= try_convert(date, e.[text], 103)
FROM OPENJSON(@RJSON) OJC
CROSS APPLY OPENJSON(OJC.[value])
with (fields nvarchar(max) as JSON) a
CROSS APPLY OPENJSON(a.[fields])
with (Total nvarchar(max) as json, MerchantName nvarchar(max) as json, TransactionDate nvarchar(max) as json) b
CROSS APPLY OPENJSON (b.Total)
with ([value] float) c
CROSS APPLY OPENJSON (b.MerchantName)
with ([value] nvarchar(max)) d
CROSS APPLY OPENJSON (b.TransactionDate)
with ([text] nvarchar(max)) e
where
OJC.[key]='understandingResults'
关于sql - Azure 表单识别对 Microsoft SQL 表的 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59112242/