简体   繁体   English

使用SQL Server 2016查询读取JSON文件-子记录问题

[英]Reading JSON file using a SQL Server 2016 query - child records issue

I have the following JSON file named ( C:\\temp\\test.json ) that has a main record and three child records structured like this: 我有一个名为( C:\\ temp \\ test.json )的以下JSON文件,它具有一个主记录和三个子记录,其结构如下:

 [{ 
  "dc:type":{ 
     "dc:title":"AMC",
     "dc:type":"recordType",
     "rdf:resource":"https://tmkl.at1/16777434"
  },
  "dc:title":"CCP00113620",
  "RM_Approved_on":"2011-12-19T13:04:36Z",
  "Title":"RAM PROGRAM PLAN",
  "Project":{ 
     "oslc_cm:label":"HTLS",
     "rdf:resource":"https://tmkl.at1/16789988-34271859"
  },
  "Drawing":"N",
  "Attachments":{ 
     "oslc_cm:results":[ 
        { 
           "rdf:about":"https://tmkl.at1/16777434-33668052/field/Attachments/attachment/16782994-34738717",
           "dc:type":{ 
              "dc:title":"AMC",
              "dc:type":"recordType",
              "rdf:resource":"https://tmkl.at1./16777434"
           },
           "dc:title":"Doc-review-sheet-CCP00113620.xls",
           "filename":"Doc-1234-CCP00113620.xls"
        },
        { 
           "rdf:about":"https://tmkl.at1/16777434-33668052/field/Attachments/attachment/16782994-34736984",
           "dc:type":{ 
              "dc:title":"AMC",
              "dc:type":"recordType",
              "rdf:resource":"https://tmkl.at1./16777434"
           },
           "dc:title":"Doc-review-sheet-CCP00113620-JD.xls",
           "filename":"Doc-CCP00113620-JD300.xls"
        },
        { 
           "rdf:about":"https://tmkl.at1/16777434-33668052/field/Attachments/attachment/16782994-34738722",
           "dc:type":{ 
              "dc:title":"AMC",
              "dc:type":"recordType",
              "rdf:resource":"https://tmkl.at1./16777434"
           },
           "dc:title":"3CU 05007 0009 UMZZA-Rev01-B_edited.doc",
           "filename":"UMZZA-Rev01-B_edited.doc"
        }
     ],
     "oslc_cm:collref":"https://tmkl.at1/16777434-33668052/field/Attachments"
  },
  "rdf:about":"https://tmkl.at1/16777434-33668052"
 },
 { 
    "dc:type":{ 
       "dc:title":"AMC",
       "dc:type":"recordType",
       "rdf:resource":"https://tmkl.at1./16777434"
    }]

I would like to read this file ( C:\\temp\\test.json ) using a sql SQL Server 2016 query and obtain the following result: 我想使用sql SQL Server 2016查询读取此文件( C:\\ temp \\ test.json ),并获得以下结果:

id                Project      Title             Filename        Web-address  
CCP00113620         HTLS       RAM PROGRAM PLAN   Doc-1234-CCP00113620.xls https://tmkl.at1/16777434-33668052/field/Attachments/attachment/16782994-34738717
CCP00113620         HTLS       RAM PROGRAM PLAN   Doc-CCP00113620-JD300.xls https://tmkl.at1/16777434-33668052/field/Attachments/attachment/16782994-34736984
CCP00113620         HTLS       RAM PROGRAM PLAN   UMZZA-Rev01-B_edited.doc  https://tmkl.at1/16777434-33668052/field/Attachments/attachment/16782994-34738722

I tried the following query : 我尝试了以下查询:

SELECT ecn_att.*,q1.* 
FROM OPENROWSET (BULK 'd:\temp\ecn_att.json', SINGLE_CLOB) as j 
CROSS APPLY OPENJSON(BulkColumn) 
WITH( 
      [id] [varchar](16), 
      [Project] [varchar](200), 
      [Title] [varchar](200), 
      [Attachments] nvarchar(max) AS JSON   
 ) AS ecn_att 
CROSS APPLY OPENJSON ([Attachments]) 
WITH (
      [dc:type] [varchar](max)
) as Q1

I am unable to get the child records 我无法获得儿童记录

This the correct query that returns the expected result. 这是返回预期结果的正确查询。 The tricky part was to enclose the name of the child element [oslc_cm:results] in double quotes: 棘手的部分是将子元素的名称[oslc_cm:results]用双引号引起来:

 SELECT ecn_att.*,q1.*  FROM OPENROWSET (BULK 'd:\temp\ecn_att.json', SINGLE_CLOB) as j
     CROSS APPLY OPENJSON(BulkColumn)
     WITH(
        [id] [varchar](16),
        [Project] [varchar](200),
        [Title] [varchar](200),
        [Attachments] nvarchar(max) AS JSON 
        ) AS ecn_att
         CROSS APPLY OPENJSON ([Attachments],'$."oslc_cm:results"') WITH ([filename] nvarchar(max), [rdf:about] nvarchar(max)) as Q1

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

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