简体   繁体   English

将查询结果导出到Json

[英]Export query result to Json

Using the FOR JSON AUTO statement, if you can with another perfect method, how can I convert the result of the select that I set as an example in the following Json format and so on with the rest of the results? 如果可以,使用FOR JSON AUTO语句,可以使用另一种完美的方法,如何转换以下面的Json格式设置为示例的select的结果,以及其他结果呢?

According to the category I would have 1 or more requirements inside of the Json: 根据类别,我将在Json内部有1个或多个需求:

  • Category 1 is formed if title has S 如果标题具有S,则形成类别1
  • Category 2 is formed if domain and origin have S 如果来源具有S,则形成类别2
  • Category 3 is formed if all the others are N but Origin is S 如果其他所有条件均为N,但原点为S,则形成类别3
  • Category 4 is formed with all the requirements in N 类别4由N中的所有要求组成

Data: 数据:

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

output: 输出:

{
 "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" ]
 },

You can build your resultset as a union of the 4 categories (each one with its rules) and then convert everything to json: 您可以将结果集构建为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

Results: 结果:

{
    "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"]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM