2

我正在尝试为我目前正在研究的一个新项目创建一个 JSON 文件,我的大部分工作都按预期工作,但我现在正处于尝试使用子查询以正确格式化 JSON。

我尝试使用以下子查询,但 SQL 不喜欢这种格式。

` 选择

'Admin User TEST ONLY PLEASE IGNORE' AS AdditionalNotes


(
   SELECT v.atFault 
   FROM dbo.ic_DP_AX ax
   CROSS APPLY (VALUES (ax.Acc_fault1), (ax.Acc_fault2)) v (atFault)
   FOR JSON AUTO
   ) AS InsuredPartyClaims,
   (
   SELECT Acc_fault3 AS atFault 
   FROM dbo.ic_DP_AX
   FOR JSON AUTO
   ) AS InsuredPartyConvictions
FOR JSON PATH) ROOT('InsuredParties')



FROM

    dbo.icp_Daprospect AS p INNER JOIN
    dbo.icp_Dapolicy AS d ON p.Branch@ = d.Branch@ AND p.ClientRef@ = 
 d.ClientRef@ LEFT OUTER JOIN
    dbo.ic_DP_AX AS  ax ON P.Branch@ = ax.B@ AND ax.PolRef@ = d.PolicyRef@ 
LEFT OUTER JOIN
WHERE 
    d.PolicyRef@ = '' AND
    d.Branch@ = 0`

对于 JSON 路径

我想要实现的输出是:

"InsuredParties": [
    {
        "InsuredPartyClaims": [
            {
                "atFault": false
            },
            {
                "atFault": true
            }
        ],
        "InsuredPartyConvictions": [ 
           {
                "atFault": false
           },
           

谁能看到我做错了什么?我试图让这个尽可能简单。

4

2 回答 2

1

没有样本数据总是很困难,但愚蠢的例子是一个可能的解决方案:

桌子:

CREATE TABLE dbo.ic_DP_AX (Acc_fault1 bit, Acc_fault2 bit, Acc_fault3 bit)
INSERT INTO dbo.ic_DP_AX (Acc_fault1, Acc_fault2, Acc_fault3)
VALUES (0, 1, 0)

声明:

SELECT
   (
   SELECT v.atFault 
   FROM dbo.ic_DP_AX ax
   CROSS APPLY (VALUES (ax.Acc_fault1), (ax.Acc_fault2)) v (atFault)
   FOR JSON AUTO
   ) AS InsuredPartyClaims,
   (
   SELECT Acc_fault3 AS atFault 
   FROM dbo.ic_DP_AX
   FOR JSON AUTO
   ) AS InsuredPartyConvictions
FOR JSON PATH, ROOT('InsuredParties')   

结果:

{
  "InsuredParties":[
    {
      "InsuredPartyClaims":[
        {
          "atFault":false
        },
        {
          "atFault":true
        }
      ],
      "InsuredPartyConvictions":[
        {
          "atFault":false
        }
      ]
    }
  ]
}
于 2020-09-17T12:02:37.830 回答
0

子查询也需要返回 JSON。

尝试

(
  (SELECT ax.Acc_fault1 AS [atFault] FROM dbo.ic_DP_AX AS ax FOR JSON PATH) AS [PartyClaims]
  (SELECT ax.Acc_fault2 AS [atFault] FROM dbo.ic_DP_AX AS ax FOR JSON PATH) AS [PartyClaims]
  (SELECT ax.Acc_fault3 AS [atFault] FROM dbo.ic_DP_AX AS ax FOR JSON PATH) AS [PartyConvictions]
) FOR JSON PATH AS [InsuredParties]
于 2020-09-17T11:48:30.620 回答