10

我有一个表格,在一列中存储 json。我想通过合并另一个 json 来更新 json 值。

就像是:

insert into mytable 
values ('{ "a": "b" ')

update mytable 
set jsonColumn = JSON_MERGE(jsonColumn, '{ "c": 2 }')

这应该导致像这样的json:

{ "a": "b", "c": 2 }

不幸的是,没有这样的JSON_MERGE功能,JSON_MODIFY让我只能一一修改列。我有太多的东西,包括嵌套属性。

我基本上是在寻找相当于postgres || 连接运算符

4

7 回答 7

3

在 Sql Server 2016 中,无法将变量用作 json 路径JSON_MODIFY,因此我不确定是否有针对此问题的优雅解决方案。

如果你有 Sql Server 2017,那么它似乎是可能的。

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;

几点注意事项:

  • 为了简单起见,我没有添加检查两个对象是否实际上是有效的 json;
  • 我不知道是否有更好的方法来检查给定的字符串是 json 数组还是 json 对象;
  • 无法使用 json_modify 添加数组的第一个元素,因此CONCAT如果第一个字符串是对象而第二个是数组,则可以回退到 simple;
  • 我必须创造性地使用JSON_QUERY函数,以便正确插入 jsons;
  • 我已经使用了这样一个事实,即如果您在SELECT语句中分配变量,那么您可以在分配语句中使用变量的先前值;

sql server fiddle demo

postgresql fiddle example

更新我添加了一些改进的版本,它应该更好地处理不同类型的值:

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

sql fiddle demo

于 2019-04-05T15:35:57.357 回答
2

您可以执行类似于该代码的操作:

DECLARE @json1 nvarchar(max),
        @json2 nvarchar(max)

DECLARE @result AS nvarchar(max)

SET @json1 = N'{"a": "1", "c": "3"}'

SET @json2 = N'{"b": "2"}'

SELECT
  @result = COALESCE(@result + ', ', '') + '"' + [key] + '":"' + value + '"'
FROM (SELECT
  [key],
  value
FROM OPENJSON(@json1)
UNION ALL
SELECT
  [key],
  value
FROM OPENJSON(@json2)) AS x

SET @result = '{' + @result + '}'

PRINT @result

@结果是

{"a":"1", "c":"3", "b":"2"}
于 2018-02-22T11:24:04.517 回答
2

聚会也有点晚了,但我们在尝试在 MS SQL 中合并 JSON 时遇到了类似的问题。我们还希望它是递归的,并允许我们为“union”、“concat”和“replace”等数组定义策略。

我们针对合并JSON 路径表达式等 JSON 操作的解决方案刚刚变成开源,现在可以在@Github上使用

请随意使用、评论和贡献,以便我们进一步改进 MS SQL 的 JSON 方法。

于 2019-10-30T17:41:59.220 回答
0

我参加聚会有点晚了,但我现在遇到了类似的事情。我根据这个问题制作了一个解决方案,它将合并顶级 JSON 项目。

这样做的一些例子:

{"a":1} + {"B":2} = {"a":1,"B":2}
{"x":true,"y":{"a":"b","c":"d"}} + {"y":{"a":"z"}} = {"x":true,"y":{"a":"z"}}

此版本不会向下钻取以合并子项(例如,在我的第二个示例中,它不会保留 ["y"]["c"] 索引)。我想这样做可以对其进行增强,但这是一个快速的概念验证版本,出于我的目的,我不需要担心这些更新。

内容:

--- Merge the top-level items of two JSON object strings into one JSON
--- based off of: https://stackoverflow.com/questions/47489030/generate-a-json-string-containing-the-differences-in-two-other-json-strings-usin

 DECLARE  @jsonA     NVARCHAR(MAX) = '{"CommonValue":"OriginalThing", "OldValue": "A", "ComplexValue": {"InnerValue": "ABC"}}'
        ,@jsonB     NVARCHAR(MAX) = '{"CommonValue":"ChangedThing", "NewValue": "B", "Number": 22}'
        ,@result    NVARCHAR(MAX) = ''

--- Catalog of differences.
DECLARE @JsonDiff TABLE 
(
    OldKey CHAR(128),
    OldValue NVARCHAR(MAX),
    OldType CHAR(1),
    NewKey CHAR(128),
    NewValue NVARCHAR(MAX),
    NewType CHAR(1)
)
--- Temporary table for output rows.
--- The table could probably clipped out for production stuff.
--- For proof-of-concept, it's useful for querying results
---  before building the JSON string.
DECLARE @JsonData TABLE 
(
    NewKey CHAR(128),
    NewValue NVARCHAR(MAX),
    NewType CHAR(1)
)

;WITH DSA AS
(
    SELECT *
    FROM OPENJSON(@jsonA)   
)
,DSB AS
(
    SELECT *
    FROM OPENJSON(@jsonB)   
)
INSERT INTO @JsonDiff (OldKey, OldValue, OldType, NewKey, NewValue, NewType)
SELECT a.[Key] aKey, a.[Value] aValue, a.[Type] aType, b.[Key] bKey, b.[Value] bValue, b.[Type] bType
FROM DSA A
FULL OUTER JOIN DSB B ON A.[key] = B.[key]

INSERT INTO @JsonData (NewKey, NewValue, NewType) 
    SELECT OldKey as k, OldValue as v, OldType as t
    FROM @JsonDiff
    WHERE OldKey IS NOT NULL AND NewKey IS NULL
    UNION
    SELECT NewKey as k, NewValue as v, NewType as t
    FROM @JsonDiff
    WHERE NewKey IS NOT NULL

--- a few queries for display purposes
--- select * FROM @JsonDiff
select NewKey, NewValue FROM @JsonData

SELECT @result += CONCAT ( '"', TRIM([NewKey]), '":'
    ,IIF([NewType] = 1, CONCAT('"', [NewValue], '"'), [NewValue]) -- If the item is a string, then add quotes.
    ,','
)
FROM @JsonData

--- Print the JSON
SELECT CONCAT('{', LEFT(@result, LEN(@result) - 1), '}')

编辑:这是最后一点的稍微简化的版本,无需拥有@JsonData

SELECT @result += CONCAT ( '"', TRIM([k]), '":'
    ,IIF([t] = 1, CONCAT('"', [v], '"'), [v]) -- If the item is a string, then add quotes.
    ,','
)
FROM 
    (
        SELECT OldKey as k, OldValue as v, OldType as t
            FROM @JsonDiff
            WHERE OldKey IS NOT NULL AND NewKey IS NULL
        UNION
        SELECT NewKey as k, NewValue as v, NewType as t
            FROM @JsonDiff
            WHERE NewKey IS NOT NULL
    ) as mid

--- Print the JSON
SELECT CONCAT('{', LEFT(@result, LEN(@result) - 1), '}')
于 2019-04-05T02:03:54.157 回答
0

还可以看看:

SELECT (
    SELECT 
    (
        SELECT    ID AS "test.id" 
        FROM      [Table1] 
        FOR       JSON AUTO
    )             AS 'test1', 
                  'id' AS 'test2'
    FROM          test2
    FOR           JSON AUTO
)                 AS JSON
于 2021-05-27T01:46:31.793 回答
0

JSON_MODIFY 中有附加的概念,但这取决于标签名称。请看下面的例子。如果你有标签名称,那么它可以工作,否则不能。从https://docs.microsoft.com/en-us/sql/t-sql/functions/json-modify-transact-sql了解更多信息。另请注意,您可以在某个时间点附加一个值

PRINT 'EXAMPLE 1

'

DECLARE @j NVARCHAR(MAX)
SET @j = '{"k" : ["a","b"] }'
PRINT @J
SET @J=JSON_MODIFY(JSON_MODIFY(@j,'append $.k','c'),'append $.k','2')
PRINT @J
GO

PRINT '
EXAMPLE 2

'

DECLARE @j NVARCHAR(MAX)
SET @j = '{"a":"b"}'
PRINT @J
SET @J=JSON_MODIFY(@J,'append $','c:2')
PRINT @J
GO

输出

EXAMPLE 1

{"k" : ["a","b"] }
{"k" : ["a","b","c","2"] }

EXAMPLE 2

{"a":"b"}
{"a":"b"}
于 2018-02-22T05:54:20.123 回答
0

基于@JorgeRibeiro 回答,如果您想编辑现有值,这也仅适用于一级 json-

DECLARE @json1 nvarchar(max),
        @json2 nvarchar(max)

DECLARE @result AS nvarchar(max)

SET @json1 = N'{"a": "1", "c": "3"}'

SET @json2 = N'{"a": "2","b" : "4"}'

SELECT
  @result = COALESCE(@result + ', ', '') + '"' + [key] + '":"' + value + '"'
FROM (SELECT
  [key],
  value
FROM OPENJSON(@json1) where [key] not in (Select Distinct [key] from OPENJSON(@json2))
UNION ALL
SELECT
  [key],
  value
FROM OPENJSON(@json2)) AS x

SET @result = '{' + @result + '}'

PRINT @result
于 2021-08-19T09:25:55.420 回答