生成SQL查询的JSON

huangapple go评论68阅读模式
英文:

Generate JSON from SQL query

问题

这是您需要的JSON字符串,已从SQL查询中提取并翻译:

{
    "DocumentType": "FFS",
    "DBSKeyData": {
        "ProducingPlant": "FRM",
        "ProductionOrder": "TEST_Order"
    },
    "DDSData": [
        {
            "Method": "Print",
            "PrinterId": "Test_ID"
        },
        {
            "Method": "Fileshare",
            "SharedLocation": "Test_Path",
            "Path": "Z\\Test",
            "FileName": "Test.pdf",
            "Overwrite": "true",
            "Watermark": "true",
            "Content": "1"
        }
    ]
}

对于您的SQL查询,似乎没有提供精确的结果。您可能需要重新检查查询以确保正确提取所需的信息。

英文:

I need a JSON string as below from the SQL query.

 {
    "DocumentType": "FFS",
    "DBSKeyData": {
        "ProducingPlant": "FRM",
        "ProductionOrder": "TEST_Order"        
    },
    "DDSData": [
        {
            "Method": "Print",
            "PrinterId": "Test_ID"                 
        },
        {
            "Method": "Fileshare",
            "SharedLocation": "Test_Path",
            "Path": "Z\\Test",
            "FileName": "Test.pdf",
            "Overwrite": "true",
            "Watermark": "true",
            "Content": "1"
        }
    ] 
}

I tried the below query to get expected JSON string:

SELECT top 1 
  labelType AS documentType,
  (
  select producingPlant,  productionOrder 
  from test T2 with (Nolock)
  where T1.ordinalid = t2.ordinalid FOR JSON Path 
  ) AS dbsKeyData, 
  (
  select Method, PrinterId, [SharedLocation], [Path] ,[FileName], [Overwrite], [Watermark], [Content]
  from (
     select ordinalid, 'Print' as Method,Printer as PrinterId ,'' [SharedLocation], '' [Path] , [FileName], '' [Overwrite], '' [Watermark], ''  [Content]
     from test T3  with (Nolock)
     union all
     select ordinalid, 'Fileshare' Method, '' PrinterId, [SharedLocation], [Path] ,[FileName] as [FileName], 'true' [Overwrite], 'true' [Watermark], 1 [Content]
     from test T4  with (Nolock)
  ) T    
  where T1.ordinalid = T.ordinalid 
  FOR JSON PATH
  ) AS DDSData

But it hasn't given exact result.

{"documentType":"FFS",
 "dbsKeyData": 
  [
  {"producingPlant":"FRM","productionOrder":"TEST_Order"}
  ],
  "DDSData": 
  [
 {
  "Method":"Print",
  "PrinterId":"Test_ID",
  "SharedLocation":"",
  "Path":"",
  "FileName":"Test.pdf",
  "Overwrite":"",
  "Watermark":"",
  "Content":0
  }, 
  {
  "Method":"Fileshare",
  "PrinterId":"",
  "SharedLocation":"Test_Path",
  "Path":"Z\\Test","FileName":"Test.pdf",
  "Overwrite":"true",
  "Watermark":"true",
  "Content":1
  }
]
}

Kindly give me some suggestions.

Thank You!

答案1

得分: 0

您可以在对基本表进行单次扫描时完成此操作。

将“Printer”和“Location”值解构成单独的行,并在子查询中进行聚合。

可以使用带有.的特殊别名创建dbsKeyData

SELECT
  t.labelType AS documentType,
  t.producingPlant AS [dbsKeyData.producingPlant],
  t.productionOrder as [dbsKeyData.productionOrder],
  (
    SELECT *
    FROM (VALUES
      ('Print', Printer, NULL, NULL, NULL, NULL, NULL, NULL),
      ('Fileshare', NULL, SharedLocation, Path, FileName, 'true', 'true', 1)
    ) v(Method, PrinterId, SharedLocation, Path, FileName, Overwrite, Watermark, Content)
    FOR JSON PATH
  ) AS DDSData
FROM test
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER;

子查询的另一种选项是

SELECT
  t.labelType AS documentType,
  t.producingPlant AS [dbsKeyData.producingPlant],
  t.productionOrder as [dbsKeyData.productionOrder],
  JSON_QUERY('[' +
  (
    SELECT
      'Print' AS Method,
      Printer AS PrinterId
    FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
  ) + ',' +
  (
    SELECT
      'Fileshare' AS Method,
      SharedLocation,
      Path,
      FileName,
      'true' AS Overwrite,
      'true' Watermark,
      1 AS Content
    FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
  ) + ']') AS DDSData
FROM test
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER;

除非您非常了解自己在做什么,否则不要使用NOLOCK

英文:

You can do this in a single scan of the base table.

Unpivot the Printer and Location values into separate rows and aggregate them in a subquery.

dbsKeyData can be created using special aliases with . in them.

SELECT
  t.labelType AS documentType,
  t.producingPlant AS [dbsKeyData.producingPlant],
  t.productionOrder as [dbsKeyData.productionOrder],
  (
    SELECT *
    FROM (VALUES
      ('Print', Printer, NULL, NULL, NULL, NULL, NULL, NULL),
      ('Fileshare', NULL, SharedLocation, Path, FileName, 'true', 'true', 1)
    ) v(Method, PrinterId, SharedLocation, Path, FileName, Overwrite, Watermark, Content)
    FOR JSON PATH
  ) AS DDSData
FROM test
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER;

Another option for the subquery is

SELECT
  t.labelType AS documentType,
  t.producingPlant AS [dbsKeyData.producingPlant],
  t.productionOrder as [dbsKeyData.productionOrder],
  JSON_QUERY('[' +
  (
    SELECT
      'Print' AS Method,
      Printer AS PrinterId
    FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
  ) + ',' +
  (
    SELECT
      'Fileshare' AS Method,
      SharedLocation,
      Path,
      FileName,
      'true' AS Overwrite,
      'true' Watermark,
      1 AS Content
    FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
  ) + ']') AS DDSData
FROM test
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER;

Do not use NOLOCK unless you really really really know what you're doing.

huangapple
  • 本文由 发表于 2023年7月18日 13:36:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709764.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定