使用 JSON_MODIFY 更新 JSON
Update JSON using JSON_MODIFY
我将 JSON 数据存储在 SQL 服务器中。
我的 table 看起来像:
Table 名称:JsonData
列:ID、数据
我的 JSON 看起来像:
{
"data": [{
"identifier": 1,
"someData": {
"sample1": "lorem 1",
"sample2": "test 1"
}
},
{
"identifier": 2,
"someData": {
"sample1": "lorem 2",
"sample2": "test 2"
}
},
{
"identifier": 3,
"someData": {
"sample1": "lorem 3",
"sample2": "test 3"
}
}
]
}
我想使用 JSON_MODIFY 更新标识符 3 的示例 1。
如何访问 sample1 并对其进行修改?
示例:
您的 JSON
是一个 JSON
对象数组,因此您需要一个索引来访问每个元素。在这种情况下,一种可能的方法是使用具有默认架构的 path
parameter for JSON_MODIFY
. Note, that this feature is available in SQL Server 2017 (14.x) and in Azure SQL Database. The JSON
array is splitted into elements using OPENJSON 表达式,在这种情况下,返回的列为 key
、value
和 type
.
基本示例:
声明:
DECLARE @json nvarchar(max) = N'{
"data": [{
"identifier": 1,
"someData": {
"sample1": "lorem 1",
"sample2": "test 1"
}
},
{
"identifier": 2,
"someData": {
"sample1": "lorem 2",
"sample2": "test 2"
}
},
{
"identifier": 3,
"someData": {
"sample1": "lorem 3",
"sample2": "test 3"
}
}
]
}'
SELECT JSON_MODIFY(@json, '$.data[' + j.[key] + '].someData.sample1', N'NewValue') AS JsonData
FROM OPENJSON(@json, '$.data') j
WHERE JSON_VALUE([value], '$.identifier') = 3
输出:
----------------------------
JsonData
----------------------------
{
"data": [{
"identifier": 1,
"someData": {
"sample1": "lorem 1",
"sample2": "test 1"
}
},
{
"identifier": 2,
"someData": {
"sample1": "lorem 2",
"sample2": "test 2"
}
},
{
"identifier": 3,
"someData": {
"sample1": "NewValue",
"sample2": "test 3"
}
}
]
}
Table 例子:
Table:
CREATE TABLE #Data (
ID int,
Data nvarchar(max)
)
INSERT INTO #Data
(ID, Data)
VALUES
(1, N'{
"data": [{
"identifier": 1,
"someData": {
"sample1": "lorem 1",
"sample2": "test 1"
}
},
{
"identifier": 2,
"someData": {
"sample1": "lorem 2",
"sample2": "test 2"
}
},
{
"identifier": 3,
"someData": {
"sample1": "lorem 3",
"sample2": "test 3"
}
}
]
}'),
(2, N'{
"data": [{
"identifier": 1,
"someData": {
"sample1": "lorem 1",
"sample2": "test 1"
}
},
{
"identifier": 2,
"someData": {
"sample1": "lorem 2",
"sample2": "test 2"
}
},
{
"identifier": 3,
"someData": {
"sample1": "lorem 3",
"sample2": "test 3"
}
}
]
}')
声明:
SELECT
d.ID, c.Data
FROM #Data d
CROSS APPLY (
SELECT JSON_MODIFY(d.Data, N'$.data[' + CONVERT(nvarchar(max), j.[key] COLLATE Latin1_General_CI_AS) + N'].someData.sample1', N'NewValue') AS Data
FROM OPENJSON(d.Data, '$.data') j
WHERE JSON_VALUE([value], '$.identifier') = 3
) c
备注:
key
列具有 BIN2 排序规则,因此您需要使用排序规则选项进行转换。
我将 JSON 数据存储在 SQL 服务器中。
我的 table 看起来像:
Table 名称:JsonData
列:ID、数据
我的 JSON 看起来像:
{
"data": [{
"identifier": 1,
"someData": {
"sample1": "lorem 1",
"sample2": "test 1"
}
},
{
"identifier": 2,
"someData": {
"sample1": "lorem 2",
"sample2": "test 2"
}
},
{
"identifier": 3,
"someData": {
"sample1": "lorem 3",
"sample2": "test 3"
}
}
]
}
我想使用 JSON_MODIFY 更新标识符 3 的示例 1。 如何访问 sample1 并对其进行修改?
示例:
您的 JSON
是一个 JSON
对象数组,因此您需要一个索引来访问每个元素。在这种情况下,一种可能的方法是使用具有默认架构的 path
parameter for JSON_MODIFY
. Note, that this feature is available in SQL Server 2017 (14.x) and in Azure SQL Database. The JSON
array is splitted into elements using OPENJSON 表达式,在这种情况下,返回的列为 key
、value
和 type
.
基本示例:
声明:
DECLARE @json nvarchar(max) = N'{
"data": [{
"identifier": 1,
"someData": {
"sample1": "lorem 1",
"sample2": "test 1"
}
},
{
"identifier": 2,
"someData": {
"sample1": "lorem 2",
"sample2": "test 2"
}
},
{
"identifier": 3,
"someData": {
"sample1": "lorem 3",
"sample2": "test 3"
}
}
]
}'
SELECT JSON_MODIFY(@json, '$.data[' + j.[key] + '].someData.sample1', N'NewValue') AS JsonData
FROM OPENJSON(@json, '$.data') j
WHERE JSON_VALUE([value], '$.identifier') = 3
输出:
----------------------------
JsonData
----------------------------
{
"data": [{
"identifier": 1,
"someData": {
"sample1": "lorem 1",
"sample2": "test 1"
}
},
{
"identifier": 2,
"someData": {
"sample1": "lorem 2",
"sample2": "test 2"
}
},
{
"identifier": 3,
"someData": {
"sample1": "NewValue",
"sample2": "test 3"
}
}
]
}
Table 例子:
Table:
CREATE TABLE #Data (
ID int,
Data nvarchar(max)
)
INSERT INTO #Data
(ID, Data)
VALUES
(1, N'{
"data": [{
"identifier": 1,
"someData": {
"sample1": "lorem 1",
"sample2": "test 1"
}
},
{
"identifier": 2,
"someData": {
"sample1": "lorem 2",
"sample2": "test 2"
}
},
{
"identifier": 3,
"someData": {
"sample1": "lorem 3",
"sample2": "test 3"
}
}
]
}'),
(2, N'{
"data": [{
"identifier": 1,
"someData": {
"sample1": "lorem 1",
"sample2": "test 1"
}
},
{
"identifier": 2,
"someData": {
"sample1": "lorem 2",
"sample2": "test 2"
}
},
{
"identifier": 3,
"someData": {
"sample1": "lorem 3",
"sample2": "test 3"
}
}
]
}')
声明:
SELECT
d.ID, c.Data
FROM #Data d
CROSS APPLY (
SELECT JSON_MODIFY(d.Data, N'$.data[' + CONVERT(nvarchar(max), j.[key] COLLATE Latin1_General_CI_AS) + N'].someData.sample1', N'NewValue') AS Data
FROM OPENJSON(d.Data, '$.data') j
WHERE JSON_VALUE([value], '$.identifier') = 3
) c
备注:
key
列具有 BIN2 排序规则,因此您需要使用排序规则选项进行转换。