【问题标题】:How to convert multiple table output as child json in Sql如何在Sql中将多个表输出转换为子json
【发布时间】:2019-07-09 11:55:16
【问题描述】:

我一直在尝试从三个表在 sql 中创建一个 json 对象

主表/实体是“订单”,其 id 为 addressId

并且进一步的地址表有countryId和stateId,它们是指

州表和国家表

orderTable

    Id  AddressId   Status
    1     1         Processed

AddressTable

Id countryId  stateId FirstName  LastName
1    5            7     John       cena


countryTable

Id   Name
5     usa


StateTable

Id   Name
7     DC

输出应该如下所示

{
  "firstName": "John",
  "lastName": "cena",
  "Country" : {
                 "name": "usa",
                 "id" : "5"
              },
  "States" :  {
                    "name": "DC",
                    "id" : "7"
            }

}

我曾尝试使用此查询并得到类似的结果,但我想从 json 中删除 [] [] 数组对象容器

[      // I want to remove this 
{

  "FirstName": "Steve",

  "LastName": "Gates",



     "country": 
 [       // I want to remove this 
      {

                  "name": "usa",
                 "id" : "5"
              }
       ],  // I want to remove this 

"states" :
         [  // I want to remove this 
           {
                    "name": "DC",
                    "id" : "7"
            }


     ]  // I want to remove this 

根据微软的博客我们可以使用

FOR JSON PATH, WITHOUT_ARRAY_WRAPPER

但是如果我使用这个,那么它不包括国家和州作为单独的子对象 所以我使用了“FOR JSON AUTO”,它给了我想要的输出,但它还为每个 json 对象添加了方括号

这是我的查询

 Select ord.*, (Select *  from Address ad Left outer join Country country on country.Id = ad.CountryId
 Left outer join State sp on sp.Id = ad.StateId  where ad.Id = ord.AddressId  FOR JSON AUTO) as AddressJson
 , (Select *  from Address ad Left outer join Country country on country.Id = ad.CountryId
 Left outer join State sp on sp.Id = ad.StateId  where ad.Id = ord.AddressId1  FOR JSON AUTO, WITHOUT_ARRAY_WRAPPER) as AddressJson2
 from [order] ord ) 

【问题讨论】:

    标签: sql json sql-server sql-server-2016


    【解决方案1】:

    我认为不需要在您的示例中使用所有子查询。我只是使用连接和点分隔的别名来获得您想要的输出。

    Format Query Results as JSON with FOR JSON

    在 PATH 模式下,您可以使用点语法 - 例如, 'Item.UnitPrice' - 格式化嵌套输出。

    然后添加 WITHOUT_ARRAY_WRAPPER 选项以删除括号。

    这是一个工作示例:

    DECLARE @AddressTable TABLE
        (
            [Id] INT
          , [countryId] INT
          , [stateId] INT
          , [FirstName] NVARCHAR(100)
          , [LastName] NVARCHAR(100)
        );
    
    DECLARE @countryTable TABLE
        (
            [Id] INT
          , [name] NVARCHAR(100)
        );
    
    DECLARE @stateTable TABLE
        (
            [Id] INT
          , [name] NVARCHAR(100)
        );
    
    INSERT INTO @AddressTable (
                                  [Id]
                                , [countryId]
                                , [stateId]
                                , [FirstName]
                                , [LastName]
                              )
    VALUES ( 1, 5, 7, N'John', N'cena' );
    
    INSERT INTO @countryTable (
                                  [Id]
                                , [name]
                              )
    VALUES ( 5, N'usa' );
    
    INSERT INTO @stateTable (
                                [Id]
                              , [name]
                            )
    VALUES ( 7, N'DC' );
    
    SELECT     [a].[FirstName]
             , [a].[LastName]
             , [ct].[name] AS 'Country.name' --dot-separated alias
             , [ct].[Id] AS 'Country.Id'
             , [st].[name] AS 'States.name'
             , [st].[Id] AS 'States.Id'
    FROM       @AddressTable [a]
    INNER JOIN @stateTable [st]
        ON [st].[Id] = [a].[stateId]
    INNER JOIN @countryTable [ct]
        ON [ct].[Id] = [a].[countryId]
    FOR JSON PATH, WITHOUT_ARRAY_WRAPPER;
    

    这会为您提供以下输出:

    {
      "FirstName": "John",
      "LastName": "cena",
      "Country": {
        "name": "usa",
        "Id": 5
      },
      "States": {
        "name": "DC",
        "Id": 7
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-14
      • 2017-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多