Hi all clever people out there.
Assume I have a JSON file with the following structure
{
"Node1": [
{
"id": "xxxxxxxxxx",
"name": "The name",
"Sub-node1": [
{
"Type": "The type",
"id": "xxxxxxxxxxxxxx",
"users": [
{
"UserType": "The main person",
"UserRights": "Owner",
"UPN": "xxxxxx@yyyyyy.com"
}
]
},
{
"Type": "The type",
"id": "yyyyyyyyyyyyyyy",
"users": [
{
"UserType": "The main main person",
"UserRights": "Admin",
"UPN": "yyyyyyyy@yyyyyy.com"
}
]
}
],
"Sub-node2": [
{
"Type": "The type",
"id": "xxxyxxxxxyxxxxxx",
"users": [
{
"UserType": "The main person",
"UserRights": "Admin",
"UPN": "xxxxxx@yyyyyy.com"
}
]
},
{
"Type": "The type",
"id": "xxxxxxxxxxxyxxyx",
"users": [
{
"UserType": "The main main person",
"UserRights": "Viewer",
"UPN": "yyyyyyyy@yyyyyy.com"
}
]
}
],
"users": [
{
"UserType": "Superman",
"UserRights": "Admin",
"UPN": "yyyyyyyy@yyyyyy.com"
}
]
}
}
Notice that there is a users node directly under the main node (Node1) and also in each sub-node. Normally I would flatten the JSON in Serverless using CROSS APPLY. Something like this:
SELECT
Node1Id, Node1_Name, Sub-node1Type, Sub-node1_id, Sub-node2Type, Sub-node2_id, UserType, UserRights, UPN
FROM
OPENROWSET(
BULK 'https://xxxxxxxx.dfs.core.windows.net/root/yyyyyyyy/zzzzzzzz/*/*/*/*/*',
FORMAT = 'CSV',
FIELDQUOTE = '0x0b',
FIELDTERMINATOR ='0x0b'
)
WITH (
jsonContent varchar(MAX)
) AS [rows]
CROSS APPLY openjson (jsonContent)
WITH (
Node1 NVARCHAR(MAX) AS JSON
)
CROSS APPLY openjson (Node1)
WITH (
Node1Id VARCHAR(50) '$.id',
Node1_Name VARCHAR(50) '$.name',
Sub-node1 NVARCHAR(MAX) AS JSON,
Sub-node2 NVARCHAR(MAX) AS JSON,
users NVARCHAR(MAX) AS JSON
)
CROSS APPLY openjson (Sub-node1)
WITH (
Sub-node1Type varchar(255) '$.Type',
Sub-node1_id varchar(255) '$.id',
users NVARCHAR(MAX) AS JSON
)
CROSS APPLY openjson (Sub-node2)
WITH (
Sub-node2Type varchar(255) '$.Type',
Sub-node2_id varchar(255) '$.id',
users NVARCHAR(MAX) AS JSON
)
CROSS APPLY openjson (users)
WITH (
UserType varchar(50) '$.UserType',
UserRights NVARCHAR(100) '$.UserRights',
UPN varchar(100) '$.UPN',
)
)
With this method I can only get one for the users nodes out as you cannot CROSS APPLY the same named node multiple times. I cannot seem to figure out a way to name the node differently when I reference it as JSON in the sub-node CROSS APPLY and I cannot figure out a way to reference users as using some kind of path ('$.sub-node1.users' was my initial thought).
Does anyone know if it´s possible to get all the different User nodes from the main node and all the sub-nodes in a single SELECT (I have done it in multiple selects with one sub-node at a time)?
Would love to hear everyone's input.
Ásgeir