将查询结果导出到Json

时间:2018-08-26 20:29:37

标签: sql json sql-server tsql

如果可以使用FOR JSON AUTO语句,如果可以使用另一种完善的方法,那么如何将以示例Json格式设置的选择结果转换为以下Json格式,以此类推呢?

根据类别,我在Json内将有1个或多个需求:

    如果标题具有S ,则形成
  • 类别1 如果来源具有S
  • ,则形成
  • 类别2 如果所有其他条件均为N但来源为S,则形成
  • 类别3
  • 类别4由N中的所有要求组成

数据:

id          Titulo Dominio Documento fiscalizacion Informe Investigacion Legajo Origen Categoria
    ----------- ------ ------- --------- ------------- ------- ------------- ------ ------ --------------------------------------------------
    953922      N      N       N         N             N       N             N      S      3
    950794      N      N       N         N             N       N             N      S      3
    957140      N      N       N         N             N       N             N      S      3
    86068       N      N       N         N             N       N             N      N      4
    93300       N      N       N         N             N       N             N      N      4
    82286       N      S       N         N             N       N             N      S      2
    92476       S      N       N         N             N       N             N      N      1

输出:

{
 "id": 86068       ,
 "Categoria": 4,
 "Requisitos": [
     "Titulo",
     "Dominio",
     "Doumento",
     "fiscalizacion",
     "Informe",
     "Investigacion",
     "Legajo",
     "Origen" ]
 },

{
 "id": 92476       ,
 "Categoria": 1,
 "Requisitos": [
     "Titulo"
     ]
 },

{
 "id": 950794       ,
 "Categoria": 3,
 "Requisitos": [
     "Origen" ]
 },

1 个答案:

答案 0 :(得分:0)

您可以将结果集构建为4个类别(每个类别及其规则)的并集,然后将所有内容转换为json:

declare @tmp table(id int, Titulo char(1), Dominio char(1), Documento char(1), fiscalizacion char(1), Informe char(1), Investigacion char(1), Legajo char(1), Origen char(1), Categoria char(1))
insert into @tmp values    
     (953922      ,'N','N','N','N','N','N','N','S',3)
    ,(950794      ,'N','N','N','N','N','N','N','S',3)
    ,(957140      ,'N','N','N','N','N','N','N','S',3)
    ,(86068       ,'N','N','N','N','N','N','N','N',4)
    ,(93300       ,'N','N','N','N','N','N','N','N',4)
    ,(82286       ,'N','S','N','N','N','N','N','S',2)
    ,(92476       ,'S','N','N','N','N','N','N','N',1)

--  Categoria = 1
select id, categoria,json_query(replace(QUOTENAME(
   case when Titulo ='S' then quotename('Titulo','"')                + ', ' else '' end 
 + case when Dominio ='S' then  quotename('Dominio','"')             + ', ' else '' end 
 + case when Investigacion ='S' then  quotename('Investigacion','"') + ', ' else '' end 
 ),', ]',']') ) as Requisitos
from @tmp 
where Categoria = 1

UNION ALL 

--Categoria = 2
    select id, categoria,json_query(replace(QUOTENAME(
 + case when Documento ='S' then  quotename('Documento','"')         + ', ' else '' end 
 + case when fiscalizacion ='S' then  quotename('fiscalizacion','"') + ', ' else '' end 
 + case when Informe ='S' then  quotename('Informe','"')             + ', ' else '' end 
 + case when Legajo ='S' then  quotename('Legajo','"')               + ', ' else '' end 
 ),', ]',']') ) as Requisitos
from @tmp where Categoria = 2

UNION ALL

--Categoria = 3 
select id, categoria,json_query(case when Origen ='S' then  quotename(quotename('Origen','"')  )  + ', ' else '' end) as Requisitos
from @tmp 
where Categoria = 3 

UNION ALL

--Categoria = 4 
select id, categoria,json_query(replace(QUOTENAME(
   case when Titulo ='N' then quotename('Titulo','"')                + ', ' else '' end 
 + case when Dominio ='N' then  quotename('Dominio','"')             + ', ' else '' end 
 + case when Documento ='N' then  quotename('Documento','"')         + ', ' else '' end 
 + case when fiscalizacion ='N' then  quotename('fiscalizacion','"') + ', ' else '' end 
 + case when Informe ='N' then  quotename('Informe','"')             + ', ' else '' end 
 + case when Investigacion ='N' then  quotename('Investigacion','"') + ', ' else '' end 
 + case when Legajo ='N' then  quotename('Legajo','"')               + ', ' else '' end 
 + case when Origen ='N' then  quotename('Origen','"')               + ', ' else '' end 
 ),', ]',']') ) as Requisitos
from @tmp where Categoria = 4

for json auto , WITHOUT_ARRAY_WRAPPER

结果:

{
    "id": 92476,
    "categoria": "1",
    "Requisitos": ["Titulo"]
},
{
    "id": 82286,
    "categoria": "2",
    "Requisitos": []
},
{
    "id": 953922,
    "categoria": "3",
    "Requisitos": ["Origen"]
},
{
    "id": 950794,
    "categoria": "3",
    "Requisitos": ["Origen"]
},
{
    "id": 957140,
    "categoria": "3",
    "Requisitos": ["Origen"]
},
{
    "id": 86068,
    "categoria": "4",
    "Requisitos": ["Titulo",
    "Dominio",
    "Documento",
    "fiscalizacion",
    "Informe",
    "Investigacion",
    "Legajo",
    "Origen"]
},
{
    "id": 93300,
    "categoria": "4",
    "Requisitos": ["Titulo",
    "Dominio",
    "Documento",
    "fiscalizacion",
    "Informe",
    "Investigacion",
    "Legajo",
    "Origen"]
}