gpt4 book ai didi

sql - Oracle JSON_EQUAL 条件的 T-SQL 等价物是什么?

转载 作者:行者123 更新时间:2023-12-05 06:55:26 27 4
gpt4 key购买 nike

  • 我有一个表,其中有一列包含 JSON 字符串。
  • 我需要查询该表以查找与给定输入 JSON 字符串匹配的行。
  • 我想忽略顺序和格式。
    • '{ "foo": "bar"}' 应该等于 '{"foo":"bar"}'
    • '{ "foo": "bar", "a": "b"}' 应该等于 '{ "a": "b", "foo": "bar"}'
  • 我想避免明确测试单个值,因为嵌套级别或属性数量可能会随时间变化。

我不熟悉 Oracle,但我看到了对它的引用 JSON_EQUAL condition这似乎正是我需要的。我没能在 T-SQL 中找到任何类似的东西。我试过使用 JSON_QUERY比较整个对象,但它似乎基本上只是进行字符串比较,因此顺序和空格的变化被视为差异。

例如下面的示例将 [AreEqual] 值返回为 0

DECLARE @SourceJSON NVARCHAR(MAX) = '{ "timestamp":"2020-11-13T15:21:03.8407089Z", "name": "foo", "some_object":{ "prop1": 3, "prop2": "string"}}'
DECLARE @TargetJSON NVARCHAR(MAX) = '{"timestamp":"2020-11-13T15:21:03.8407089Z", "name": "foo", "some_object":{ "prop1": 3, "prop2": "string"}}'

SELECT
x.[source]
,x.[target]
,CASE WHEN x.[source] = x.[target] THEN 1 ELSE 0 END AS [AreEqual]

FROM(
SELECT
JSON_QUERY(@SourceJSON) AS [source]
,JSON_QUERY(@TargetJSON) AS [target]
) AS x

有没有办法通过 T-SQL 实现这一点?

最佳答案

JSON_EQUAL 没有直接等价物,所以我认为您必须自己动手。有一个有用的例子 here我开始研究一个例子。 OPENJSON 允许您将 JSON 转换为表格格式,而无需指定各个字段名称。

我想说的是,这只是一个仅在几个样本上进行了简单测试的示例,更多的是作为概念验证。它不是生产就绪的代码,需要进行彻底的测试。也就是说,它可能会为您提供一种比较 JSON 的方法:

-- Create an inline table-valued function that shreds JSON
CREATE OR ALTER FUNCTION dbo.tvf_shredJSON
(
@inputJSON NVARCHAR(MAX)
)
RETURNS TABLE AS RETURN
(
WITH cte AS (
SELECT
CAST( 1 AS TINYINT ) xlevel,
ROW_NUMBER() OVER( ORDER BY ( SELECT NULL ) ) rn,
*,
CAST( NULL AS INT ) AS parentRowId
FROM OPENJSON( @inputJSON, '$' )

UNION ALL

SELECT
CAST( xlevel + 1 AS TINYINT ),
ROW_NUMBER() OVER( ORDER BY ( SELECT NULL ) ) rn,
CAST( j.[key] AS NVARCHAR(4000) ),
CAST( j.[value] AS NVARCHAR(MAX) ),
CAST( j.[type] AS TINYINT ),
CAST( c.rn AS INT ) AS parentRowId
FROM cte c
CROSS APPLY OPENJSON ( c.[value] ) j
WHERE c.[type] In ( 4, 5 )
AND c.xlevel = xlevel
)
SELECT *
FROM cte
WHERE [type] Not In ( 4, 5 ) -- array (4) or object (5)
)
GO


-- Create a stored proc to receive the two JSONs and compare them
CREATE OR ALTER PROC dbo.usp_compareJSON

@sourceJSON NVARCHAR(MAX),
@targetJSON NVARCHAR(MAX),
@isEqual BIT OUTPUT,
@debug BIT = 0
AS
SET NOCOUNT ON

DECLARE @diffFound1 BIT, @diffFound2 BIT

-- Shred the two pieces of JSON to temp tables for comparison
CREATE TABLE #tmp (
jsonSource VARCHAR(20) NOT NULL,
rowId INT IDENTITY PRIMARY KEY,
xlevel TINYINT NOT NULL,
rn INT NOT NULL,
[key] NVARCHAR(4000) NOT NULL,
[value] NVARCHAR(MAX) NOT NULL,
[type] INT NOT NULL,
parentRowId INT NULL
)


-- Shred source JSON to table
INSERT INTO #tmp ( jsonSource, xlevel, rn, [key], [value], [type], parentRowId )
SELECT 'source', xlevel, rn, [key], [value], [type], parentRowId
FROM dbo.tvf_shredJSON ( @sourceJSON )


-- Shred target JSON to table
INSERT INTO #tmp ( jsonSource, xlevel, rn, [key], [value], [type], parentRowId )
SELECT 'target', xlevel, rn, [key], [value], [type], parentRowId
FROM dbo.tvf_shredJSON ( @targetJSON )


-- Comparison 1: source to target
IF EXISTS (
SELECT xlevel, [key], [value], [type]
FROM #tmp
WHERE jsonSource = 'source'
EXCEPT
SELECT xlevel, [key], [value], [type]
FROM #tmp
WHERE jsonSource = 'target'
)
BEGIN
SET @diffFound1 = 1
END
ELSE
SET @diffFound1 = 0


-- Comparison 2: target to source
IF EXISTS (
SELECT xlevel, [key], [value], [type]
FROM #tmp
WHERE jsonSource = 'target'
EXCEPT
SELECT xlevel, [key], [value], [type]
FROM #tmp
WHERE jsonSource = 'source'
)
BEGIN
SET @diffFound2 = 1
END
ELSE
SET @diffFound2 = 0


-- Return the result
IF @diffFound1 = 0 AND @diffFound2 = 0
SET @isEqual = 1
ELSE
SET @isEqual = 0


-- Show the json differences if required
IF @debug = 1
BEGIN
SELECT xlevel, [key], [value], [type], parentRowId
FROM #tmp
WHERE jsonSource = 'source'
EXCEPT
SELECT xlevel, [key], [value], [type], parentRowId
FROM #tmp
WHERE jsonSource = 'target'


SELECT xlevel, [key], [value], [type], parentRowId
FROM #tmp
WHERE jsonSource = 'target'
EXCEPT
SELECT xlevel, [key], [value], [type], parentRowId
FROM #tmp
WHERE jsonSource = 'source'
END

RETURN
GO

-- JSON is the same except for spacing
DECLARE @SourceJSON NVARCHAR(MAX) = '{ "timestamp":"2020-11-13T15:21:03.8407089Z", "name": "foo", "some_object":{ "prop1": 3, "prop2": "string"}}'
DECLARE @TargetJSON NVARCHAR(MAX) = '{"timestamp":"2020-11-13T15:21:03.8407089Z", "name": "foo", "some_object":{ "prop1": 3, "prop2": "string"}}'
DECLARE @isEqual BIT

EXEC dbo.usp_compareJSON @SourceJSON, @TargetJSON, @isEqual OUTPUT, @debug = 1

SELECT @isEqual isEqual
GO


-- JSON is the same except for ordering
DECLARE @SourceJSON NVARCHAR(MAX)
DECLARE @TargetJSON NVARCHAR(MAX)
DECLARE @isEqual BIT

SET @SourceJSON = '{ "timestamp":"2020-11-13T15:21:03.8407089Z", "name": "foo", "some_object":{ "foo": "bar", "a": "b" }}'
SET @TargetJSON = '{"timestamp":"2020-11-13T15:21:03.8407089Z", "name": "foo", "some_object":{ "a": "b", "foo": "bar" }}'

EXEC dbo.usp_compareJSON @SourceJSON, @TargetJSON, @isEqual OUTPUT, @debug = 1

SELECT @isEqual isEqual
GO



DECLARE @inputJSON NVARCHAR(MAX) =
'{
"id": "WakefieldFamily",
"parents": [
{ "familyName": "Wakefield", "givenName": "Robin" },
{ "familyName": "Miller", "givenName": "Ben" }
],
"children": [
{
"familyName": "Merriam",
"givenName": "Jesse",
"gender": "female",
"grade": 1,
"pets": [
{ "givenName": "Goofy" },
{ "givenName": "Shadow" }
]
},
{
"familyName": "Miller",
"givenName": "Lisa",
"gender": "female",
"grade": 8 }
],
"address": { "state": "NY", "county": "Manhattan", "city": "NY" },
"creationDate": 1431620462,
"isRegistered": false
}'


DECLARE @SourceJSON NVARCHAR(MAX) = @inputJSON
DECLARE @TargetJSON NVARCHAR(MAX) = @inputJSON
DECLARE @isEqual BIT

EXEC dbo.usp_compareJSON @SourceJSON, @TargetJSON, @isEqual OUTPUT, @debug = 1

SELECT @isEqual isEqual


-- Deliberately alter the JSON
SET @SourceJSON = @inputJSON
SET @TargetJSON = JSON_MODIFY( @SourceJSON, '$.children[0].pets[0].givenName', 'wBob' )

EXEC dbo.usp_compareJSON @SourceJSON, @TargetJSON, @isEqual OUTPUT, @debug = 1

SELECT @isEqual isEqual
GO

我能够根据原始的 JSON_EQUAL 脚本进行一些进一步的测试并得到相同的结果。看这个gist获取完整脚本。

关于sql - Oracle JSON_EQUAL 条件的 T-SQL 等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65343781/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com