2

我有一个带有内部属性数组的 json 文档。在这些属性之一上,键名动态/随机更改。除了最后一个讨厌的属性之外,我可以轻松提取所有数据点。我过去发现或使用 OPENJSON 的所有方法都依赖于已知的键名。

在“内部”数组中,第一个属性的键名会发生变化。我想提取与该动态键关联的值,但不知道该键将是什么。希望下面的代码能比我用文字更好地描述这个问题。

这是为了便于阅读而格式化的 JSON 文档的样子...

{
    "outer1": {
        "inner1": {
            "dynamicKey123": "attribute1",
            "staticKey1": "attribute2",
            "staticKey2": "attribute3",
            "staticKey3": "attribute4"
        }
    },
    "outer2": {
        "inner2": {
            "dynamicKeyABC": "attribute1",
            "staticKey1": "attribute2",
            "staticKey2": "attribute3",
            "staticKey3": "attribute4"
        }
    }
}

一些要测试的代码...

CREATE TABLE openjson_test (json_col VARCHAR(MAX));

INSERT INTO openjson_test (json_col)
VALUES ('{"outer1":{"inner1":{"dynamicKey123":"attribute1","staticKey1":"attribute2","staticKey2":"attribute3","staticKey3":"attribute4"}},"outer2":{"inner2":{"dynamicKeyABC":"attribute1","staticKey1":"attribute2","staticKey2":"attribute3","staticKey3":"attribute4"}}}');

到目前为止,我开发的带有麻烦部分的查询已注释掉...

SELECT
    json_col,
    so.[key] AS soKey,
    si.[key] AS siKey,
    si.[value] AS siValue,
    --ar.dynamicKey, 
    ar.staticKey1,
    ar.staticKey2,
    ar.staticKey3
FROM openjson_test
CROSS APPLY OPENJSON(json_col) so
CROSS APPLY OPENJSON(json_col, '$.' + so.[key]) si
CROSS APPLY OPENJSON(json_col, '$.' + so.[key] + '.' + si.[key])
WITH (
    --dynamicKey VARCHAR(256) '$.dynamicKey???', How do I extract this value without knowing the key
    staticKey1 VARCHAR(256) '$.staticKey1',
    staticKey2 VARCHAR(256) '$.staticKey2',
    staticKey3 VARCHAR(256) '$.staticKey3'
) ar
4

2 回答 2

0

我建议使用条件聚合的方法

SELECT
    so.[key] AS soKey,
    si.[key] AS siKey,
    MAX(CASE WHEN attr.[key] NOT IN('staticKey1','staticKey2','staticKey3') THEN attr.[value] END) AS DynamicAttr,
    MAX(CASE WHEN attr.[key]='staticKey1' THEN attr.[value] END) AS attrKey1,
    MAX(CASE WHEN attr.[key]='staticKey2' THEN attr.[value] END) AS attrKey2,
    MAX(CASE WHEN attr.[key]='staticKey3' THEN attr.[value] END) AS attrKey3
FROM openjson_test
CROSS APPLY OPENJSON(json_col) so
CROSS APPLY OPENJSON(json_col, '$.' + so.[key]) si
CROSS APPLY OPENJSON(json_col, '$.' + so.[key] + '.' + si.[key]) attr
GROUP BY so.[key],si.[key];

此技术用于PIVOT场景中,但允许更通用的逻辑。

于 2019-04-17T06:23:10.213 回答
0

您可以OPENJSON不使用该WITH子句并过滤掉具有已知名称的列:

SELECT json_col,
    so.[key] AS soKey,
    si.[key] AS siKey,
    si.[value] AS siValue,
    ar2.Value AS dynamicKey,
    ar.staticKey1,
    ar.staticKey2,
    ar.staticKey3
FROM dbo.openjson_test t
CROSS APPLY OPENJSON(t.json_col) so
CROSS APPLY OPENJSON(json_col, '$.' + so.[key]) si
CROSS APPLY OPENJSON(json_col, '$.' + so.[key] + '.' + si.[key])
WITH (
    staticKey1 VARCHAR(256) '$.staticKey1',
    staticKey2 VARCHAR(256) '$.staticKey2',
    staticKey3 VARCHAR(256) '$.staticKey3'
) ar
CROSS APPLY (
    SELECT *
    FROM OPENJSON(json_col, '$.' + so.[key] + '.' + si.[key])
    WHERE [Key] NOT IN ('staticKey1','staticKey2','staticKey3')
) ar2
于 2019-04-17T04:55:50.980 回答