【问题标题】:How to structure JSON SQL by row using values from the row?如何使用行中的值按行构造 JSON SQL?
【发布时间】:2018-10-24 16:18:53
【问题描述】:

我正在努力使用 SQL 构建 JSON。

假设我有一张这样的桌子:

| col1          | col2     | col3     |
+---------------+----------+----------+
| specialvalue  | someval  | someval  |
| specialvalue2 | someval2 | someval2 |
|               |          |          |

我正在尝试获得如下结构:

{
    "specialvalue": {
        "specialcol": "specialvalue",
        "col2": "someval",
        "col3": "someval"
    },
    "specialvalue2": {
        "specialcol": "specialvalue2",
        "col2": "someval2",
        "col3": "someval2"
    }
}

我怎样才能做到这一点?我可以将 JSON_MODIFY 与动态键一起使用,同时映射集合中的每一行吗?

我得到的最接近的是:

SELECT
     specialcol,
     col2,
     col3
INTO #tmpTbl
FROM myTable

SELECT 
    specialcol, 
    (SELECT * FROM #tmpTbl FOR JSON AUTO) as 'Value' 
FROM #tmpTbl 
FOR JSON AUTO

DROP TABLE #tmp

返回以下内容:

{
    "specialcol":"specialvalue",
    "Value":{
        "col1": "specialvalue",
        "col2": "someval",
        "col3": "someval"
    },
    "specialcol":"specialvalue2",
    "Value":{
        "col1": "specialvalue2",
        "col2": "someval2",
        "col3": "someval2"
    }
}

这很接近,但不是我需要的。

有没有办法使用 JSON_MODIFY 来完成我想要得到的东西?

【问题讨论】:

  • 我不这么认为。您可以通过对已有的内容进行一些调整来接近 FOR JSON,但是当您需要基于 col1 中的值时,您的顶级“键”最终会被命名为相同。不完全相同,但情况相似,他们最终没有使用 FOR JSON stackoverflow.com/questions/46168258/…

标签: sql json sql-server


【解决方案1】:

根据我的评论和思考。不是最优雅的,但有可能让你到达那里。

内联查询以 json 格式获取结果,然后根据 col1 中的值作为键包装这些结果。把它们放在一起,用逗号分隔,最后用括号括起来。

DECLARE @TestData TABLE
    (
        [col1] NVARCHAR(100)
      , [col2] NVARCHAR(100)
      , [col3] NVARCHAR(100)
    );

DECLARE @JsonValue NVARCHAR(MAX) = '';

INSERT INTO @TestData (
                          [col1]
                        , [col2]
                        , [col3]
                      )
VALUES ( 'specialvalue', 'someval', 'someval' )
     , ( 'specialvalue2', 'someval2', 'someval2' );


SELECT @JsonValue = @JsonValue + N'"' + [a].[col1] + N'": '
                    + (
                          SELECT [aa].[col1] AS 'specialvalue'
                               , [aa].[col2] AS 'col2'
                               , [aa].[col3] AS 'col3'
                          FROM   @TestData [aa]
                          WHERE  [aa].[col1] = [a].[col1]
                          FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
                      ) + N','
FROM   @TestData [a];

SET @JsonValue = N'{' + SUBSTRING(@JsonValue, 1, LEN(@JsonValue) - 1) + N'}';
SELECT @JsonValue;

给你这个:

{
  "specialvalue": {
    "specialvalue": "specialvalue",
    "col2": "someval",
    "col3": "someval"
  },
  "specialvalue2": {
    "specialvalue": "specialvalue2",
    "col2": "someval2",
    "col3": "someval2"
  }
}

【讨论】:

  • 我喜欢。我的意思是肯定它有点 hacky,但我能想到的其他解决方案是执行动态 sql 和遍历临时表,这也是 hacky,但性能很差。这是一个更简单、更高效的方法。谢谢!
【解决方案2】:

SQL Server 2017 -

;WITH cte as (
select 'specialvalue'  as specialcol,  'someval'  col2, 'someval'  col3 union 
select 'specialvalue2' as specialcol,  'someval2' col2, 'someval2' col3 )
SELECT 
CASE WHEN specialcol = 'specialvalue' THEN specialcol END as [specialvalue.specialcol],
CASE WHEN specialcol = 'specialvalue' THEN col2 END as [specialvalue.col2],
CASE WHEN specialcol = 'specialvalue' THEN col3 END as [specialvalue.col3],
CASE WHEN specialcol = 'specialvalue2' THEN specialcol END as [specialvalue2.specialcol],
CASE WHEN specialcol = 'specialvalue2' THEN col2 END as [specialvalue2.col2],
CASE WHEN specialcol = 'specialvalue2' THEN col3 END as [specialvalue2.col3]
FROM cte
FOR JSON PATH

输出:

{
"specialvalue":{
    "specialcol":"specialvalue",
    "col2":"someval",
    "col3":"someval"
   }
},
{
"specialvalue2":{
    "specialcol":"specialvalue2",
    "col2":"someval2",
    "col3":"someval2"
    }
}

【讨论】:

  • 这不是一个可行的解决方案 - 您实际上只是硬编码了我在示例中提供的键 'specialvalue' 和 'specialvalue2'。这些值可以是任何值,我不能使用硬编码值作为键。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-24
  • 1970-01-01
  • 2017-05-04
  • 1970-01-01
  • 1970-01-01
  • 2015-10-15
  • 2021-11-03
相关资源
最近更新 更多