SQL-FOR JSON PATH层次结构

时间:2018-12-10 15:13:14

标签: json sql-server tsql

我很难在SQL中使用正确的层次结构正确地格式化JSON。通过各种搜索,似乎答案是嵌套的子查询,每个子查询都有自己的FOR JSON PATH,但是当我尝试执行此操作时,无法以正确的方式获得与子查询相关的避免数据重复的方法。

此查询返回我的SQL数据集。

SELECT 
      Ctype.[name] as "Checklists.checklistType"
    , Sections.[name] as "Sections.sectionName"
    , Details.mx as "Questions.textValue"
    , Tasks.taskName as "Questions.question"

FROM
        ChecklistHeader Header INNER JOIN
        ChecklistDetails Details ON Header.ID = Details.FK_checklistHeaderID INNER JOIN 
        ChecklistType CType ON CType.ID = Header.FK_checklistTypeID INNER JOIN
        ChecklistSections Sections ON Sections.FK_checklistTypeID = Ctype.ID INNER JOIN
        ChecklistTasks Tasks ON Tasks.ID = Details.FK_checklistTaskID AND Tasks.FK_checklistSectionID = Sections.ID 

WHERE
Header.FK_assetID = 289

这将返回如下数据:

Checklists.checklistType    Sections.sectionName    Questions.answerValue   Questions.question
Launcher PreOperational     WALK AROUND             366                     redacted1
Launcher PreOperational     WALK AROUND             366                     redacted2
Launcher PreOperational     WALK AROUND             366                     redacted3
Launcher PreOperational     WALK AROUND             366                     redacted4
Launcher PreOperational     WALK AROUND             366                     redacted5
Launcher PreOperational     SETTINGS PRESSURES      366                     redacted6
Launcher PreOperational     SETTINGS PRESSURES      366                     redacted7
Launcher PreOperational     SETTINGS PRESSURES      366                     redacted8
Launcher PreOperational     SETTINGS PRESSURES      366                     redacted9
Launcher PreOperational     SETTINGS PRESSURES      366                     redacted10

我的要求是然后以这种格式生成JSON:

{
  "Checklists": [
    {
      "Sections": [
        {
          "Questions": [
            {
              "answerValue": "366",
              "question": "redacted1"
            },
            {
              "answerValue": "366",
              "question": "redacted2"
            },
            {
              "answerValue": "366",
              "question": "redacted3"
            },
            {
              "answerValue": "366",
              "question": "redacted4"
            },
            {
              "answerValue": "366",
              "question": "redacted5"
            }
          ],
          "sectionName": "WALK AROUND"
        },
        {
          "Questions": [
            {
              "answerValue": "366",
              "question": "redacted6"
            },
            {
              "answerValue": "366",
              "question": "redacted7"
            },
            {
              "answerValue": "366",
              "question": "redacted8"
            },
            {
              "answerValue": "366",
              "question": "redacted9"
            },
            {
              "answerValue": "366",
              "question": "redacted10"
            }
          ],
          "sectionName": "SETTINGS PRESSURES"
        }
      ],
      "checklistType": "Preflight Checklist 30"
    }
  ]
}

我该如何实现?

0 个答案:

没有答案