SQL Server 2016将JSON解析为表

时间:2018-02-16 21:45:13

标签: json sql-server tsql sql-server-2016

我正在尝试使用SQL Server 2016中的JSON解析将API的JSON输出转换为SQL Server表。但是,我很难让它工作。我已经筋疲力尽了所有的搜索工作。

JSON输出非常简单。

    [{"Jobs":[{"Agricultural":false,"AlternateTitle":null,"DateInJob":"/Date(1454997600000)/","DirectLabor":false,"EffectiveDate":"/Date(-62135575200000)/","EmployeeIdentifier":{"EmployeeNumber":"000001","CompanyCode":"60639"},"EmployeeType":"REG","FullOrPartTime":"F","HourlyOrSalaried":"S","JobCode":"HRCOORD","JobGroup":null,"LocalUnion":null,"NationalUnion":null,"OrgLevel1":null,"OrgLevel2":null,"OrgLevel3":null,"OrgLevel4":null,"PayFrequency":"B","PayGroup":"60639","PayScaleCode":null,"Project":null,"Promotion":false,"ReasonCode":"100","ScheduledHours":80,"Seasonal":false,"SelfServiceProperties":null,"ShiftCode":"Z","ShiftGroup":"Z","StepNo":null,"Supervisor":null,"TimeClock":null,"Transfer":false,"YouthTraining":false}],"CompanyCode":"60639","EmployeeNumber":"000001","FirstName":"George","LastName":"TestEmployee"},{"Jobs":[{"Agricultural":false,"AlternateTitle":"Client Care Representative","DateInJob":"/Date(1497592800000)/","DirectLabor":false,"EffectiveDate":"/Date(-62135575200000)/","EmployeeIdentifier":{"EmployeeNumber":"003613","CompanyCode":"60637"},"EmployeeType":"TES","FullOrPartTime":"F","HourlyOrSalaried":"H","JobCode":"CCREP","JobGroup":null,"LocalUnion":null,"NationalUnion":null,"OrgLevel1":"000","OrgLevel2":"001","OrgLevel3":null,"OrgLevel4":null,"PayFrequency":"B","PayGroup":"60637","PayScaleCode":null,"Project":"STAFF","Promotion":false,"ReasonCode":"Z","ScheduledHours":80,"Seasonal":false,"SelfServiceProperties":null,"ShiftCode":"Z","ShiftGroup":"Z","StepNo":null,"Supervisor":{"EmployeeNumber":"003639","CompanyCode":"60637","ExtensionData":{}},"TimeClock":null,"Transfer":false,"YouthTraining":false}],"CompanyCode":"60637","EmployeeNumber":"003613","FirstName":"George","LastName":"TestEmployee"}]

我已经使用这个SQL代码将JSON数据拉入SQL。

    Declare @JSON nvarchar(max) 
    SELECT @JSON = BulkColumn-- Replace(Replace(BulkColumn,'',''),'','')
    FROM OPENROWSET (BULK 'C:\JSON\JobService.json', SINGLE_CLOB) as j

    Select *
    from OPENJSON(@JSON, N'$')
    WITH (
    LastName nvarchar(100) N'$.LastName'
    ,FirstName nvarchar(100) N'$.FirstName'
    ,ScheduledHours INT N'$.Jobs.ScheduledHours')

我遇到的问题是获得更深层次的嵌套数据。 LastName和FirstName很好,但我无法在“Jobs”对象中获得任何内容或更深层次,我无法弄清楚原因。任何帮助将非常感谢。

1 个答案:

答案 0 :(得分:2)

您可以使用APPLY再次调用OPENJSON,如下所示:

DECLARE @JSON NVARCHAR(MAX)=' [{"Jobs":[{"Agricultural":false,"AlternateTitle":null,"DateInJob":"/Date(1454997600000)/","DirectLabor":false,"EffectiveDate":"/Date(-62135575200000)/","EmployeeIdentifier":{"EmployeeNumber":"000001","CompanyCode":"60639"},"EmployeeType":"REG","FullOrPartTime":"F","HourlyOrSalaried":"S","JobCode":"HRCOORD","JobGroup":null,"LocalUnion":null,"NationalUnion":null,"OrgLevel1":null,"OrgLevel2":null,"OrgLevel3":null,"OrgLevel4":null,"PayFrequency":"B","PayGroup":"60639","PayScaleCode":null,"Project":null,"Promotion":false,"ReasonCode":"100","ScheduledHours":80,"Seasonal":false,"SelfServiceProperties":null,"ShiftCode":"Z","ShiftGroup":"Z","StepNo":null,"Supervisor":null,"TimeClock":null,"Transfer":false,"YouthTraining":false}],"CompanyCode":"60639","EmployeeNumber":"000001","FirstName":"George","LastName":"TestEmployee"},{"Jobs":[{"Agricultural":false,"AlternateTitle":"Client Care Representative","DateInJob":"/Date(1497592800000)/","DirectLabor":false,"EffectiveDate":"/Date(-62135575200000)/","EmployeeIdentifier":{"EmployeeNumber":"003613","CompanyCode":"60637"},"EmployeeType":"TES","FullOrPartTime":"F","HourlyOrSalaried":"H","JobCode":"CCREP","JobGroup":null,"LocalUnion":null,"NationalUnion":null,"OrgLevel1":"000","OrgLevel2":"001","OrgLevel3":null,"OrgLevel4":null,"PayFrequency":"B","PayGroup":"60637","PayScaleCode":null,"Project":"STAFF","Promotion":false,"ReasonCode":"Z","ScheduledHours":80,"Seasonal":false,"SelfServiceProperties":null,"ShiftCode":"Z","ShiftGroup":"Z","StepNo":null,"Supervisor":{"EmployeeNumber":"003639","CompanyCode":"60637","ExtensionData":{}},"TimeClock":null,"Transfer":false,"YouthTraining":false}],"CompanyCode":"60637","EmployeeNumber":"003613","FirstName":"George","LastName":"TestEmployee"}]'

SELECT * FROM OPENJSON(@JSON)
WITH (
    LastName nvarchar(100) N'$.LastName',
    FirstName nvarchar(100) N'$.FirstName',
    Jobs NVARCHAR(MAX) AS JSON
) j1
OUTER APPLY (
    SELECT * FROM OPENJSON(j1.Jobs)
    WITH (
        ScheduledHours INT '$.ScheduledHours'
    )
) x