json - SQL Server:从另一个 json 更新 json——通常
问题描述
在 SQL Server 中,如何在不显式使用键/定义列的情况下将 json 与另一个 json 更新/合并?
一些背景知识:我将元数据作为 json 存储在varchar(max)
列中。每条记录在同一个表中可以有不同的元数据键。就像将人员和产品存储在同一张表中一样。类似于EAV 数据模型,但我使用 json 列将元数据存储为键值对,而不是值表。这就是为什么我正在寻找一个通用的解决方案。
即一个记录可以有元数据
{"last_name":"John","first_name":"Smith","age":28,"Address":"123 Steels st…"}
同一张表中的另一条记录可以有元数据
{"product_name":"Box","material":"plastic","Price":1.5,"Weight":20,"Height":15}
我正在寻找一种高效/现代的方式来从 json 更新/添加 json 中的多个值。
即来源
{
"last_name": "John",
"first_name": "Smith",
"age": 28,
"weight":79
"address": "123 Steels st…"
}
要更新/添加的内容:
{
"address": "567 Yonge Ave…"
"last_name": "Johnny"
"age": 35
"height":1.83
}
结果源更新为:
{
"last_name":"Smith",
"first_name": "Johnny", - updated
"age": 35, - updated
"weight":79
"address": "567 Yonge Ave…" - updated
"height":1.83 - added
}
我的解决方案:
declare @j_source varchar(200) = '{"first_name": "Smith", "last_name": "Smith","age": 28,"weight":79,"address": "123 Steels st…"}'
declare @j_update varchar(200) = '{"address": "567 Yonge Ave…","first_name": "Johnny","age": 35, "height":1.83}'
print @j_source
print @j_update
-- transform json to tables
select *
into #t_source
from openjson(@j_source)
select *
into #t_update
from openjson(@j_update)
-- combine the updated values with new values with non-updated values
select *
into #t_result
from
(
-- get key values that are not being updated
select ts.[key],ts.[value],ts.[type]
from #t_source as ts
left join #t_update as tu
on ts.[key] = tu.[key]
where tu.[key] is null
union -- get key values that are being updated. side note: the first and second select can be combined into one using isnull
select ts.[key],tu.[value],ts.[type] -- take value from #t_update
from #t_source as ts
inner join #t_update as tu
on ts.[key] = tu.[key]
union -- add new key values that does not exists in the source
select tu.[key],tu.[value],tu.[type] -- take value from #t_update
from #t_source as ts
right join #t_update as tu
on ts.[key] = tu.[key]
where ts.[key] is null
) as x
where [value] != '' -- remove key-value pair if the value is empty
/*
openjson type column data type
https://docs.microsoft.com/en-us/sql/t-sql/functions/openjson-transact-sql?view=sql-server-2017
type data-type
0 null
1 string
2 int
3 true/false
4 array
5 object
*/
-- transform table back to json in a generic way
select @j_source =
'{' +
STUFF((
select replace(',"x":','x', cast([key] as varchar(4000)) COLLATE SQL_Latin1_General_CP1_CI_AS)
+ case [type]
when 1 then replace('"z"','z',[value]) -- this is a string this is a text use double-quotes
when 2 then [value] -- this is int, don't use double-quotes
else ''
end
from #t_result
for xml PATH('')
), 1, 1, '')
+ '}'
print 'after update'
print @j_source
drop table #t_source
drop table #t_update
drop table #t_result
我的解决方案有效,但是:
可能不适用于数组或嵌套 json。好吧,暂时不打扰我。
我想知道是否有更合适/情感/优雅的方式来完成整个解决方案,也许使用 json_modify ?
键值对的顺序没有保留为源,但我想这没什么大不了的。
在没有明确定义列并且没有“for json auto”给出的“垃圾”的情况下将键值表转换回json的任何正常方法?
代码:
SELECT [key], [value]
FROM t_result
FOR JSON path, WITHOUT_ARRAY_WRAPPER
输出:
{"key":"address","value":"567 Yonge Ave…"},
{"key":"age","value":35}, {"key":"first_name","value":"Johnny"},
{"key":"height","value":1.83},{"key":"last_name","value":"Smith"}
更新:
基于Roman Pekar 优雅的解决方案,我在该解决方案中添加了另一个案例,以在值为 [type] = 2(int) 时排除引号。当像我这样有数百万条记录时,额外的报价会影响存储。
create function dbo.fn_json_merge
(
@a nvarchar(max),
@b nvarchar(max)
)
returns nvarchar(max)
as
begin
if left(@a, 1) = '{' and left(@b, 1) = '{'
begin
select
@a = case
when d.[type] in (1,3) then json_modify(@a, concat('$.',d.[key]), d.[value])
else @a
end,
@a = case
when d.[type] in (2) and TRY_CAST(d.[value] AS int) is not null then json_modify(@a, concat('$.',d.[key]), cast(d.[value] as int))
when d.[type] in (2) and TRY_CAST(d.[value] AS int) is null then json_modify(@a, concat('$.',d.[key]), d.[value])
else @a
end,
@a = case
when d.[type] in (4,5) then json_modify(@a, concat('$.',d.[key]), json_query(d.[value]))
else @a
end
from openjson(@b) as d;
end
else if left(@a, 1) = '[' and left(@b, 1) = '{'
begin
select @a = json_modify(@a, 'append $', json_query(@b));
end
else
begin
select @a = concat('[', @a, ',', right(@b, len(@b) - 1));
end;
return @a;
end;
解决方案
看看这个答案。如果您在 Sql Server 2017 中工作,那么您可以创建函数来合并 json:
create function dbo.fn_json_merge
(
@a nvarchar(max),
@b nvarchar(max)
)
returns nvarchar(max)
as
begin
if left(@a, 1) = '{' and left(@b, 1) = '{' begin
select
@a = case when d.[type] in (4,5) then json_modify(@a, concat('$.',d.[key]), json_query(d.[value])) else @a end,
@a = case when d.[type] not in (4,5) then json_modify(@a, concat('$.',d.[key]), d.[value]) else @a end
from openjson(@b) as d;
end else if left(@a, 1) = '[' and left(@b, 1) = '{' begin
select @a = json_modify(@a, 'append $', json_query(@b));
end else begin
select @a = concat('[', @a, ',', right(@b, len(@b) - 1));
end;
return @a;
end;
根据评论更新更新 - 应该更好地使用不同类型的值
create function dbo.fn_json_merge
(
@a nvarchar(max),
@b nvarchar(max)
)
returns nvarchar(max)
as
begin
if left(@a, 1) = '{' and left(@b, 1) = '{' begin
select @a =
case
when d.[type] in (4,5) then
json_modify(@a, concat('$.',d.[key]), json_query(d.[value]))
when d.[type] in (3) then
json_modify(@a, concat('$.',d.[key]), cast(d.[value] as bit))
when d.[type] in (2) and try_cast(d.[value] as int) = 1 then
json_modify(@a, concat('$.',d.[key]), cast(d.[value] as int))
when d.[type] in (0) then
json_modify(json_modify(@a, concat('lax $.',d.[key]), 'null'), concat('strict $.',d.[key]), null)
else
json_modify(@a, concat('$.',d.[key]), d.[value])
end
from openjson(@b) as d
end else if left(@a, 1) = '[' and left(@b, 1) = '{' begin
select @a = json_modify(@a, 'append $', json_query(@b))
end else begin
select @a = concat('[', @a, ',', right(@b, len(@b) - 1))
end
return @a
end
推荐阅读
- android - 地理围栏 PendingIntent 始终为空
- python - 使用新列更新熊猫数据框
- interface - 在 BPMN 中 - 如何对两个组件之间的详细信息(字段)接口进行建模?
- docker - 如何在 docker 上共享挂载目录的所有权?
- c# - 没有设置器,DataTrigger EnterActions 不起作用?
- hierarchy - 获取父子层次结构
- python-3.x - 为什么在下面示例中的 for 循环中,两个示例中的每个示例中的第二个 for 循环中的结果都不完全相同
- c - 我想要默认参数提升的例子
- javascript - 如何在输出期间更改.js文件变量的背景颜色
- c - fread 和 fwrite 如何区分 C 中的不同数据(类型)?