Share via

Synapse Serverless nested JSON in SQL view

Ásgeir Gunnarsson 61 Reputation points
2021-12-13T14:55:01.407+00:00

Hi

I´m using Synapse Pipelines to read data from the groups endpoint in Microsoft Graph. The output is something like this (of course with many more groups):

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#groups",
    "@odata.nextLink": "https://graph.microsoft.com/v1.0/groups?$skiptoken=RFNwdAIAAQAAACpHcm91cF8wNWU0MGRhZC03YWFjLTQyNGEtYWM3Zi0yMjE1YWRhZTI2NjAqR3JvdXBfMDVlNDBkYWQtN2FhYy00MjRhLWFjN2YtMjIxNWFkYWUyNjYwAAAAAAAAAAAAAAA",
    "value": [
        {
            "id": "12345678-1234-1234-1234-012345678912",
            "deletedDateTime": null,
            "classification": null,
            "createdDateTime": "2021-02-02T08:50:21Z",
            "creationOptions": [
                "Team",
                "ExchangeProvisioningFlags:3552"
            ],
            "description": "Group",
            "displayName": "GroupName",
            "groupTypes": [
                "Unified"
            ],
            "preferredLanguage": null,
            "resourceBehaviorOptions": [
                "HideGroupInOutlook",
                "SubscribeMembersToCalendarEventsDisabled",
                "WelcomeEmailDisabled"
            ],
            "resourceProvisioningOptions": [
                "Team"
            ]
        }
}

I´m trying to create a query in Serverless that can list some of the attributes as I want it exposed as a view. But I´m failing to get anything.

Since the fields are inside the value tag I tried this to get the id column only (as a test):

select JSON_VALUE (doc, '$.value.id') AS GroupId

from openrowset(
         BULK 'Link to file in data lake',
        format = 'csv',
        fieldterminator ='0x0b',
        fieldquote = '0x0b'
    ) with (doc nvarchar(max)) as rows

But that gives me just nulls.

Does anyone have an idea how to get the nested values (I would love to get the next level down as well to filter only on unified groups).

Thanks in advance,
Ásgeir Gunnarsson

Azure Synapse Analytics
Azure Synapse Analytics

An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.

0 comments No comments

Answer accepted by question author

SQLArcher 81 Reputation points
2021-12-14T00:32:02.353+00:00

Hi @Ásgeir Gunnarsson

You need to identify the depth you want to navigate to. See below for an example, and notice the value [0]:

SELECT TOP 100  
      
     JSON_VALUE(r.jsonContent, '$.value[0].id') as id,  
     JSON_VALUE(r.jsonContent, '$.value[0].deletedDateTime') as deletedDateTime,  
     JSON_VALUE(r.jsonContent, '$.value[0].classification') as classification,  
     JSON_VALUE(r.jsonContent, '$.value[0].createdDateTime') as createdDateTime  
  
FROM  
    OPENROWSET(  
        BULK '<link>',  
        FORMAT = 'CSV',  
        FIELDQUOTE = '0x0b',  
        FIELDTERMINATOR ='0x0b',  
        ROWTERMINATOR = '0x0b'  
    )  
    WITH (  
        jsonContent varchar(MAX)  
    ) AS [r]  

More info here: https://learn.microsoft.com/en-us/sql/relational-databases/json/json-path-expressions-sql-server?view=sql-server-ver15#PATH

If this answers you question, then please remember to mark this as Answered.

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.