如何从json中提取这些值
考虑到这一点:如何从json中提取这些值,json,sql-server,Json,Sql Server,考虑到这一点: { "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1", "title": "One or more validation errors occurred.", "status": 400, "traceId": "|a134a743-4f46942d1
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|a134a743-4f46942d175af9d6.",
"errors": {
"$.payment[0].invoiceDate": [
"The JSON value could not be converted to System.Nullable`1[System.Int32]. Path: $.payment[0].invoiceDate | LineNumber: 0 | BytePositionInLine: 533."
]
}
}
如何提取$.payment[0].invoiceDate和JSON值。。。变成变量
使用SQL Server 2016。这里有三种不同的方法
declare @json nvarchar(max)=N'{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|a134a743-4f46942d175af9d6.",
"errors": {
"$.payment[0].invoiceDate": [
"The JSON value could not be converted to System.Nullable`1[System.Int32]. Path: $.payment[0].invoiceDate | LineNumber: 0 | BytePositionInLine: 533."
]
}
}';
/* #1 without OPENJSON */
select json_value(@json, '$.errors."$.payment[0].invoiceDate"[0]') newCol;
/* #2 with OPENJSON and JSON_VALUE */
select json_value(value, '$[0]') newCol
from openjson(@json,'$.errors');
/* #3 with OPENJSON */
select value newCol
from openjson(@json,'$.errors."$.payment[0].invoiceDate"');
每个输出都是相同的
newCol
The JSON value could not be converted to System.Nullable`1[System.Int32]. Path: $.payment[0].invoiceDate | LineNumber: 0 | BytePositionInLine: 533.
我的尝试在第一步就失败了。只需从中选择JSON_VALUEvalue“$.errors”作为例外IDOPENJSON@json,以“$.errors”为例;给出消息13607,级别16,状态4,第18行JSON路径未正确格式化。在位置2处发现意外字符。选择*,json_valuevalue,$[0]”来自openjson@json,“$错误”