使用SQL解析存储在SQL Server对象中的JSON数组

时间:2018-08-09 11:15:11

标签: sql json sql-server

我正在尝试使用SQL获取JSON 数组的值

SELECT JSON_QUERY(CAST([JSON_Field] AS NVARCHAR(MAX)), '$contactDetails.name') 
FROM users 

我正在获取所有NULL值。

3 个答案:

答案 0 :(得分:0)

由于您提供了任何示例,所以我只举一个可能对您有帮助的示例

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

答案 1 :(得分:0)

尝试一下:

declare @json2 nvarchar(max)=N'[{"DateCreated":"2015-11-08", "DateModified":"2017-01-23", "name":"John", "Id":"b325b4"}]'

SELECT * FROM OPENJSON(@JSON2)
WITH (
    [name] nvarchar(100),
    [DateCreated] nvarchar(100) 
) j1

看看这个链接:https://docs.microsoft.com/en-us/sql/t-sql/functions/openjson-transact-sql?view=sql-server-2017#examples

答案 2 :(得分:0)

从您在评论中发布的json片段开始:

{
    "ContactDetails": [{
        "DateCreated": "2015-11-08",
        "DateModified": "2017-01-23",
        "name": "John",
        "Id": "b325b4"
    }]
}

您可以使用openjson读取外部元素,然后可以使用cross apply和另一个openjson解析内部数组的内容,这一次为每一列指定名称和数据类型您想阅读:

declare @json nvarchar(max) = '{"ContactDetails":[ {"DateCreated":"2015-11-08", "DateModified":"2017-01-23", "name":"John", "Id":"b325b4"} ]}'

select s.* 
from openjson(@json)
with (
    [ContactDetails] nvarchar(max) as json
) t
cross apply(
    select *
    from openjson(t.ContactDetails)
    with (
        [Id]           nvarchar(50) '$.Id',
        [name]         nvarchar(50) '$.name',
        [DateCreated]  date         '$.DateCreated', 
        [DateModified] date         '$.DateModified'
    )
) s

结果:

enter image description here