How can I correctly translate this complicated JSON into a SQL table? - sql

I have the following JSON (some people may recall I've posted here before about a care management system I'm extracting info from in JSON form) to then use in an automated process of KPI gathering. A single resident strand of this particular JSON export is below, though there is one for each resident contained within the extract:
{
"ServiceUserDetails": [
{
"CellDetails": [
{
"CareNotes": []
},
{
"CareNotes": []
},
{
"CareNotes": []
},
{
"CareNotes": []
},
{
"CareNotes": []
},
{
"CareNotes": []
},
{
"CareNotes": []
},
{
"CareNotes": []
},
{
"CareNotes": []
},
{
"CareNotes": []
},
{
"CareNotes": []
},
{
"CareNotes": []
},
{
"CareNotes": []
},
{
"CareNotes": []
},
{
"CareNotes": []
},
{
"CareNotes": []
},
{
"CareNotes": []
},
{
"CareNotes": []
},
{
"CareNotes": []
},
{
"CareNotes": [
{
"careNoteID": "00000000-0000-0000-0000-000000000000",
"CareAttempted": true,
"hour": null,
"DateDone": "2019-10-20T18:51:36.966Z",
"worker": null,
"sliderOpt": 0,
"DisplayAnswerType": 1
}
]
},
{
"CareNotes": []
},
{
"CareNotes": []
},
{
"CareNotes": []
},
{
"CareNotes": []
},
{
"CareNotes": [
{
"careNoteID": "00000000-0000-0000-0000-000000000000",
"CareAttempted": true,
"hour": null,
"DateDone": "2019-10-25T12:55:02.006Z",
"worker": null,
"sliderOpt": 3,
"DisplayAnswerType": 1
}
]
},
{
"CareNotes": []
},
{
"CareNotes": []
},
{
"CareNotes": [
{
"careNoteID": "00000000-0000-0000-0000-000000000000",
"CareAttempted": true,
"hour": null,
"DateDone": "2019-10-28T13:42:42.136Z",
"worker": null,
"sliderOpt": 2,
"DisplayAnswerType": 1
},
{
"careNoteID": "00000000-0000-0000-0000-000000000000",
"CareAttempted": true,
"hour": null,
"DateDone": "2019-10-28T22:29:06.226Z",
"worker": null,
"sliderOpt": 4,
"DisplayAnswerType": 1
}
]
},
{
"CareNotes": [
{
"careNoteID": "00000000-0000-0000-0000-000000000000",
"CareAttempted": true,
"hour": null,
"DateDone": "2019-10-29T13:52:32.961Z",
"worker": null,
"sliderOpt": 7,
"DisplayAnswerType": 1
}
]
},
{
"CareNotes": []
},
{
"CareNotes": [
{
"careNoteID": "00000000-0000-0000-0000-000000000000",
"CareAttempted": true,
"hour": null,
"DateDone": "2019-10-31T16:36:05.347Z",
"worker": null,
"sliderOpt": 4,
"DisplayAnswerType": 1
},
{
"careNoteID": "00000000-0000-0000-0000-000000000000",
"CareAttempted": true,
"hour": null,
"DateDone": "2019-10-31T17:53:42.264Z",
"worker": null,
"sliderOpt": 2,
"DisplayAnswerType": 1
}
]
}
],
"ServiceUserName": "Resident Name",
"ServiceUserID": "45k045839-d88b-436b-92b3-8829dce683be",
"ServiceUserDateOfBirth": "16/01/30"
}
]
}
Basically, this is data regarding a number of days when a resident has potentially been for a number two! In this single example, the person in question is independent and only occasionally requires assistance, and the data export shows each day in the month, with the majority of "CareNotes" being empty to reflect this.
I have created the following query in SQL:
use CMUtility;
DECLARE #JSON VARCHAR(MAX)
SELECT #JSON = BulkColumn
FROM OPENROWSET
(BULK 'C:\Users\user\Documents\Embarcadero\Studio\Projects\CMUServer\Win32\Debug\BowelMovementChart-Home Name.json', SINGLE_CLOB)
AS j
DROP TABLE IF EXISTS [JSONBMs-Home Name]
SELECT * INTO [JSONBMs-Home Name]
FROM
OPENJSON(#JSON, '$.ServiceUserDetails')
WITH
(
ServiceUserName nvarchar(100) '$.ServiceUserName',
ServiceUserID nvarchar(255) '$.ServiceUserID',
ServiceUserDateOfBirth nvarchar(10) '$.ServiceUserDateOfBirth',
CareNotes nvarchar(max) '$.CellDetails' as JSON
) as j1
CROSS APPLY OPENJSON(j1.CareNotes) WITH
(
CareNoteID nvarchar(200) '$.CareNoteID',
CareAttempted nvarchar(10) '$.CareAttempted',
[hour] nvarchar(50) '$.hour',
DateDone nvarchar(10) '$.DateDone',
worker nvarchar(100) '$.worker',
sliderOpt nvarchar(10) '$.sliderOpt',
DisplayAnswerType nvarchar(10) '$.DisplayAnswerType'
) j2
SELECT * FROM [JSONBMs-Home Name]
If I run the code as is, I get the following result (1302 rows):
But if I quit at the end of the j1, before the cross apply and then run a select * from table, I get the following 42 rows (which is the number of residents):
It is obviously in the second half of this code that I am going wrong. In other JSON parsing, I've had to use OUTER APPLY rather than CROSS APPLY, but interchanging them has no impact on the results. The CareNotes JSON fields remain NULL, and I think it is to do with the number of additional elements in here? 🤷‍♂️ I still don't 100% understand the difference between objects and arrays within JSON, and this has puzzled me for the last 10hours or so!
My understanding would be that to get to the additional JSON in CareNotes, I would need to point from ServiceUserDetails > CellDetails > CareNotes, but it is at this point I come unstuck. If I add ", '$.CareNotes'" to the second half's OPENJSON, then I get no results at all. I would appreciate any assistance!!
Thanks
Ant
EDIT: I've added an additional layer of cross apply, to make it now look like this, and I do now get results in the last variables:
select * into [JSONBMs-Home Name]
from
OPENJSON(#JSON, '$.ServiceUserDetails')
with
(
ServiceUserName nvarchar(100) '$.ServiceUserName',
ServiceUserID nvarchar(255) '$.ServiceUserID',
ServiceUserDateOfBirth nvarchar(10) '$.ServiceUserDateOfBirth',
CellDetails nvarchar(max) '$.CellDetails' as JSON
) as j1
cross apply openjson(j1.CellDetails) with
(
CareNotes nvarchar(max) '$.CareNotes' as JSON
) as j2
cross apply openjson(j2.CareNotes) with
(
CareNoteID nvarchar(200) '$.CareNoteID',
CareAttempted nvarchar(10) '$.CareAttempted',
[hour] nvarchar(50) '$.hour',
DateDone nvarchar(10) '$.DateDone',
worker nvarchar(100) '$.worker',
sliderOpt nvarchar(10) '$.sliderOpt',
DisplayAnswerType nvarchar(10) '$.DisplayAnswerType'
) j3
select * from [JSONBMs-Home Name]
My results are now like this:
I think this is now what I was looking for, because if then order the output by ServiceUserName it shows me the individual entries that appear in the report itself. So I think I've answered my own question!

as a rule of thumb, every time there is a json array an apply operator is needed.
since CareNotes is an array, you'll need an APPLY on the CareNotes to get its elements.
SELECT *
FROM
OPENJSON(#JSON, '$.ServiceUserDetails')
WITH
(
ServiceUserName nvarchar(100) '$.ServiceUserName',
ServiceUserID nvarchar(255) '$.ServiceUserID',
ServiceUserDateOfBirth nvarchar(10) '$.ServiceUserDateOfBirth',
CareNotes nvarchar(max) '$.CellDetails' as JSON
) as j1
OUTER APPLY OPENJSON(j1.CareNotes) --, '$')
WITH
(
CareNotesElements nvarchar(max) '$.CareNotes' AS JSON
) as notearray
OUTER APPLY OPENJSON(notearray.CareNotesElements) WITH
(
CareNoteID nvarchar(200) '$.careNoteID', ---typo here, json has careN.. ,query had CareN...
CareAttempted nvarchar(10) '$.CareAttempted',
[hour] nvarchar(50) '$.hour',
DateDone nvarchar(10) '$.DateDone',
worker nvarchar(100) '$.worker',
sliderOpt nvarchar(10) '$.sliderOpt',
DisplayAnswerType nvarchar(10) '$.DisplayAnswerType'
) j2

Because I hadn't 'drilled down' low enough, I needed to add another cross apply to get to the JSON itself.
This is my final code, which does more or less give me the data I need. Apologies if this was obvious to some, but this isn't my day job and I'm learning as I go!
Thanks
use CMUtility;
DECLARE #JSON VARCHAR(MAX)
SELECT #JSON = BulkColumn
FROM OPENROWSET
(BULK 'C:\Users\user\Documents\Embarcadero\Studio\Projects\CMUServer\Win32\Debug\BowelMovementChart-Home Name.json', SINGLE_CLOB)
AS j
drop table if exists [JSONBMs-Home Name]
select * into [JSONBMs-Home Name]
from
OPENJSON(#JSON, '$.ServiceUserDetails')
with
(
ServiceUserName nvarchar(100) '$.ServiceUserName',
ServiceUserID nvarchar(255) '$.ServiceUserID',
ServiceUserDateOfBirth nvarchar(10) '$.ServiceUserDateOfBirth',
CellDetails nvarchar(max) '$.CellDetails' as JSON
) as j1
cross apply openjson(j1.CellDetails) with
(
CareNotes nvarchar(max) '$.CareNotes' as JSON
) as j2
cross apply openjson(j2.CareNotes) with
(
CareNoteID nvarchar(200) '$.CareNoteID',
CareAttempted nvarchar(10) '$.CareAttempted',
[hour] nvarchar(50) '$.hour',
DateDone nvarchar(10) '$.DateDone',
worker nvarchar(100) '$.worker',
sliderOpt nvarchar(10) '$.sliderOpt',
DisplayAnswerType nvarchar(10) '$.DisplayAnswerType'
) j3
select * from [JSONBMs-Home Name]
order by ServiceUserName

Related

SQL Server OPENJSON read nested JSON same key array

I am following the idea from Ed.Schavelev on article 'SQL Server OPENJSON read nested json'. But I need instead of repeating the rows I wanted them (4) to be in the columns. Is this possible?
I have tried as below:
DECLARE #json NVARCHAR(MAX)
SET #json =
N'[
{
"EmployeeScheduleXRefCode":"#DF_10212",
"EmployeeXRefCode":"7274",
"TimeStart":"2022-10-31T05:30:00",
"TimeEnd":"2022-10-31T15:30:00",
"Published":false,
"Breaks":[
],
"Activities":[
],
"Skills":[
],
"LaborMetrics":[
{
"CodeXRefCode":"MEDIUM",
"TypeXRefCode":"ACUITY_LM"
},
{
"CodeXRefCode":"ATA",
"TypeXRefCode":"EMPLOYEE_TYPE_LM"
},
{
"CodeXRefCode":"BM02KR",
"TypeXRefCode":"TEAM_NAME_LM"
},
{
"CodeXRefCode":"AV_LM",
"TypeXRefCode":"JOB_TYPE_LM"
}
],
"Segments":[
]
},
{
"EmployeeScheduleXRefCode":"#DF_10200",
"EmployeeXRefCode":"7269",
"TimeStart":"2022-10-31T05:30:00",
"TimeEnd":"2022-10-31T15:30:00",
"Published":false,
"Breaks":[
],
"Activities":[
],
"Skills":[
],
"LaborMetrics":[
{
"CodeXRefCode":"MEDIUM",
"TypeXRefCode":"ACUITY_LM"
},
{
"CodeXRefCode":"ATA",
"TypeXRefCode":"EMPLOYEE_TYPE_LM"
},
{
"CodeXRefCode":"BM01KR",
"TypeXRefCode":"TEAM_NAME_LM"
},
{
"CodeXRefCode":"AV_LM",
"TypeXRefCode":"JOB_TYPE_LM"
}
],
"Segments":[
]
}
]'
SELECT Shifts.*,LaborMetrics.* FROM
OPENJSON ( #json )
WITH (
EmployeeScheduleXRefCode varchar(200) '$.EmployeeScheduleXRefCode' ,
EmployeeXRefCode varchar(200) '$.EmployeeXRefCode',
TimeStart Datetime '$.TimeStart',
TimeEnd Datetime '$.TimeEnd',
LaborMetrics nvarchar(max) as json
) as Shifts
cross apply openjson (Shifts.LaborMetrics)
with
(
CodeXRefCode nvarchar(100),
TypeXRefCode nvarchar(100)
) as LaborMetrics
But the result is repeating 4 rows and I want them to have them in 4 different columns in a single row.

Select from json data is resulting in null values. How to select from this simple json data

I have the following JSON data:
declare #json nvarchar(max)=
'
[{
"images": [{
"key": "c:\\stapler\\booktest\\gray00024.jp2",
"imageNumber": 1
}],
"instrumentNumber": 94109416
},
{
"images": [{
"key": "c:\\stapler\\booktest\\gray00025.jp2",
"imageNumber": 1
},
{
"key": "c:\\stapler\\booktest\\gray00026.jp2",
"imageNumber": 2
}
],
"instrumentNumber": 94109417
},
{
"images": [{
"key": "c:\\stapler\\booktest\\gray00027.jp2",
"imageNumber": 1
},
{
"key": "c:\\stapler\\booktest\\gray00028.jp2",
"imageNumber": 2
}
],
"instrumentNumber": 94109418
}
]'
I am trying to pull the key and imageNumber from each of these records.
I have tried this query which will give me the instrument number, but gives NULL skey and imageNumber values:
select * from openjson (#json)
with(
instrumentNumber nvarchar(100) '$.instrumentNumber',
skey nvarchar(500) '$.images.key',
imageNumber nvarchar(500) '$.images.imageNumber')
Can anyone tell me what I am doing wrong?
Because you have a nested array (images) you need to extract those values in another openjson which you can then cross apply to the original:
select a.instrumentNumber, b.skey, b.imageNumber
from openjson (#json)
with (
instrumentNumber nvarchar(100) '$.instrumentNumber',
images nvarchar(max) as json
) a
cross apply openjson (a.images)
with (
skey nvarchar(500) '$.key',
imageNumber nvarchar(500) '$.imageNumber'
) b
Output for your sample data
instrumentNumber skey imageNumber
94109416 c:\stapler\booktest\gray00024.jp2 1
94109417 c:\stapler\booktest\gray00025.jp2 1
94109417 c:\stapler\booktest\gray00026.jp2 2
94109418 c:\stapler\booktest\gray00027.jp2 1
94109418 c:\stapler\booktest\gray00028.jp2 2
Demo on dbfiddle.uk

insert json to sql table getting null

I have the following json , when i am trying to extract it to sql i get empty.
I need CusP.id, CusP.custldId ,cusfield.id, value
DECLARE #json NVARCHAR(MAX);
SELECT #json = '{
"includ": {
"cusP": {
"542310": {
"id": 542310,
"custldId": 155,
"cusfield": {
"id": 155,
"type": "custfi"
},
"projectId": 17435,
"project": {
"id": 17435,
"type": "projects"
},
"value": "META DATA",
"createdAt": "2022-01-16T05:11:20Z",
"createdBy": 222222
},
"21000": {
"id": 21000,
"custldId": 426,
"cusfield": {
"id": 426,
"type": "custfi"
},
"projectId": 786044,
"project": {
"id": 786044,
"type": "projects"
},
"value": "delta55",
"createdAt": "2022-01-17T10:03:07Z",
"createdBy": 333333
}
}
}
}'
This is what i am trying:
SELECT
D.cusPid,
d.[value],
c.cusfieldid,
cd.projectId
FROM OPENJSON(#json, '$.includ.cusP')
WITH (
cusPid NVARCHAR(max) '$.id',
[value] NVARCHAR(max) '$.value'
) D
CROSS APPLY OPENJSON(#json, '$.includ.cusP.custfi')
WITH (
cusfieldid VARCHAR(100) '$.id'
) C
CROSS APPLY OPENJSON(#json, '$.includ.cusP.project')
WITH (
projectId VARCHAR(100) '$.id'
) Cd;
that is the result i expect
cusPid
value
cusfieldid
projectId
542310
META DATA
155
17435
21000
delta55
426
786044
The problem is that the ID is also itself used as the key for a sub-property and OPENJSON does not allow variable paths (beyond arrays), so you need an extra level:
SELECT P.id AS cuspID, P.[value], P.cusfieldid, [projectId]
FROM OPENJSON(#json, '$.includ.cusP') J
CROSS APPLY OPENJSON(J.[value]) WITH (
id INT,
[value] NVARCHAR(MAX),
[projectId] INT,
cusfieldid INT '$.cusfield.id'
) P

Parse JSON data and insert into multiple tables in SQL Server

I have a JSON which contains the customer information as following format
{
"customerdata": [
{
"customerID": "abcsd112-1234-4c01-abcd-bb2fb084dc52",
"customerDetails": [
{
"fieldId": "100",
"fieldValue": "ABC"
},
{
"fieldId": "101",
"fieldValue": "TESTCUSTOMER001"
},
{
"fieldId": "102",
"fieldValue": "1000"
},
{
"fieldId": "103",
"fieldValue": "TESTNAME"
}
]
},
{
"customerID": "cdfsd112-4c01-45d7-abcd-9c9662d4ca30",
"customerDetails": [
{
"fieldId": "100",
"fieldValue": "CDE"
},
{
"fieldId": "101",
"fieldValue": "TESTCUSTOMER002"
},
{
"fieldId": "102",
"fieldValue": "1002"
},
{
"fieldId": "103",
"fieldValue": "TESTNAME2"
}
]
}
]
}
From this JSON data, I want insert data into two separate tables. In such a way that The customer ID detail to one table (CustomerDetails) and customer field details (customerDetails json node) to another table (CustomerFieldData) with foreign key reference to the first table(CustomerID).
So following are the table required
CustomerDetails table structure where customerID from JSON should be inserted to CustomerUniqueGUID column and customer name will be from field id 101 of customerDetails json node
CREATE TABLE [CustomerDetails](
[CustomerID] [bigint] IDENTITY(1,1) NOT NULL PRIMARY KEY,
[CustomerUniqueGUID] UNIQUEIDENTIFIER NOT NULL,
[CustomerName] [varchar](100) NULL,
[IsActive] [bit] NULL)
CustomerFieldData Table
CREATE TABLE [CustomerFieldData](
[CustomerFieldDataID] [bigint] IDENTITY(1,1) NOT NULL PRIMARY KEY,
[CustomerID] [bigint] NOT NULL FOREIGN KEY REFERENCES [CustomerDetails] ([CustomerID]),
[FieldID] [varchar](100) NOT NULL,
[FieldValue] [varchar](100) NOT NULL)
And I have created a sample stored procedure to do the same.
ALTER PROCEDURE [InsertCustomerInfo]
#CustomerJson NVarchar(MAX)
AS
BEGIN
INSERT INTO CustomerDetails (CustomerUniqueGUID,CustomerName)
SELECT Customer.customerID, Fields.fieldValue FROM
OPENJSON(#CustomerJson)
WITH ([customerdata] nvarchar(max) as json
) AS CustomerBatch
cross apply openjson (CustomerBatch.[customerdata])
with
(
customerID varchar(100),
customerDetails nvarchar(max) as json
) AS Customer
cross apply openjson (Customer.customerDetails) with
(
fieldId nvarchar(100),
fieldValue nvarchar(max)
) as Fields
WHERE Fields.fieldID='101'
AND CustomerDetails.CustomerUniqueGUID not in (SELECT CustomerUniqueGUID FROM CustomerDetails WHERE ISActive=1 )
If ##ROWCOUNT > 0
BEGIN
INSERT INTO [CustomerFieldData](CustomerID,FieldID,FieldValue) SELECT L.CustomerID,Fields.fieldId,Fields.fieldValue FROM
CustomerDetails L INNER JOIN
OPENJSON(#CustomerJson)
WITH ([customerdata] nvarchar(max) as json
) AS CustomerBatch
cross apply openjson (CustomerBatch.[customerdata])
with
( customerID varchar(100),
customerDetails nvarchar(max) as json
) AS Customer
cross apply openjson (Customer.customerDetails) with
(
fieldId nvarchar(100),
fieldValue nvarchar(max)
) as Fields
ON L.CustomerUniqueGUID=Customers.customerID
END
END
But data to CustomerFieldData is not happening properly and it is taking more time. Is there any issue with my script? please help.
You can use an INSERT with an OUTPUT here to get the value of both your new ID and the GUID:
DECLARE #JSON nvarchar(MAX) = N'{
"customerdata": [
{
"customerID": "abcsd112-1234-4c01-abcd-bb2fb084dc52",
"customerDetails": [
{
"fieldId": "100",
"fieldValue": "ABC"
},
{
"fieldId": "101",
"fieldValue": "TESTCUSTOMER001"
},
{
"fieldId": "102",
"fieldValue": "1000"
},
{
"fieldId": "103",
"fieldValue": "TESTNAME"
}
]
},
{
"customerID": "cdfsd112-4c01-45d7-abcd-9c9662d4ca30",
"customerDetails": [
{
"fieldId": "100",
"fieldValue": "CDE"
},
{
"fieldId": "101",
"fieldValue": "TESTCUSTOMER002"
},
{
"fieldId": "102",
"fieldValue": "1002"
},
{
"fieldId": "103",
"fieldValue": "TESTNAME2"
}
]
}
]
}';
CREATE TABLE #CustomerIDs (CustomerID bigint,
CustomerUniqueGUID varchar(100)) ;
INSERT INTO dbo.CustomerDetails (CustomerUniqueGUID,CustomerName)
OUTPUT inserted.CustomerID, inserted.CustomerUniqueGUID
INTO #CustomerIDs
SELECT OJ.customerID,
CD.fieldValue
FROM OPENJSON(#JSON,'$.customerdata')
WITH(customerID varchar(100),
customerDetails nvarchar(MAX) AS JSON) OJ
CROSS APPLY OPENJSON(OJ.customerDetails)
WITH(fieldId int,
fieldValue varchar(100)) CD
WHERE CD.fieldID = 101;
INSERT INTO dbo.CustomerFieldData (CustomerID,FieldID,FieldValue)
SELECT CI.CustomerID,
CD.fieldID,
CD.fieldValue
FROM OPENJSON(#JSON,'$.customerdata')
WITH (customerID varchar(100), --Let's use the right data type again
customerDetails nvarchar(MAX) AS JSON) OJ
CROSS APPLY OPENJSON(OJ.customerDetails)
WITH(fieldId int,
fieldValue varchar(100)) CD
JOIN #CustomerIDs CI ON OJ.customerID = CI.CustomerUniqueGUID
DROP TABLE #CustomerIDs;
GO
SELECT *
FROM dbo.CustomerDetails;
SELECT *
FROM dbo.CustomerFieldData;

SQL query OpenJson with deep nested arrays loop

I know there a lot of similar questions and answers here. I've read most of them but I'm unable to query nested arrays in a JSON structure. I'm lost in the CROSS APPLY's.
I'm actually querying a web API but for the sake of my question I've put it in a variable.
I'm trying to insert the ID (3519) and all the info of "worker_contracts" into a table.
declare #json nvarchar(max)
set #json = '
{
"data": [
{
"id": "3519",
"type": "affiliate_workers",
"attributes": {
"title": "mr",
"first_name": "John",
"last_name": "Doe",
"telephone": "+32 471 12 34 56",
"worker_contracts": [
{
"start_date": "2020-01-06",
"end_date": null,
"social_secretary_specific_data": {
"affiliate_id": 54,
"worker_details_id": 3378,
"start_date": "2020-01-06",
"end_date": null,
"affiliate_worker_id": 3519,
},
"work_hours_per_week": 25.0,
"comment": "",
"roster_week": [
{
"start_date": "2020-01-13",
"number_hours": 25
},
{
"start_date": "2020-01-06",
"number_hours": 25
}
],
"social_secretary_identifier": "123456"
},
{
"start_date": "2019-09-23",
"end_date": "2020-01-05",
"social_secretary_specific_data": {
"affiliate_id": 54,
"worker_details_id": 3378,
"start_date": "2019-09-23",
"end_date": "2020-01-05",
"affiliate_worker_id": 3519,
},
"work_hours_per_week": 21.0,
"comment": "",
"roster_week": [
{
"start_date": "2019-09-30",
"number_hours": 21
},
{
"start_date": "2019-09-23",
"number_hours": 21
}
],
"social_secretary_identifier": "123456"
}
],
"sodexo_reference": "56789",
"region": "Vlaanderen",
"identity_card_number": "A1122334455",
"can_work_with_animals": false
}
}
]
}
My SQL query only selects the start and end date values. I don't understand how I can add the other nested data.
insert into AffiliateWorkersContract_WRK
select WA.id, wc.*
from OpenJson((CAST(#json as nvarchar(max))),'$.data')
WITH (
id int,
worker_contracts nvarchar(max) as json
)
as WA
cross apply openjson (WA.worker_contracts)
with
(
start_date date '$.start_date',
end_date date '$.end_date',
) as WC
Thank you all for your help in advance.
Found the solution myself. I just had to keep going with the cross apply
insert into AffiliateWorkersContract_WRK
select WA.id, wawc.start_date, wawc.end_date, wawc.signing_date, wawc.contract_variability,
wawc.vehicle_type, wawc.addendum, wawc.work_hours_per_week,wawc.comment, wawc.social_secretary_identifier,
wssd.affiliate_id, wssd.worker_details_id, wssd.created_at, wssd.updated_at, wssd.affiliate_worker_id, wssd.comment, wssd.author_id,
wssd.parent_id, wssd.operating_headquarter_id, wssd.reference_period, wssd.varvar_ref_period_avg_minutes_per_week,
wssd.varvar_min_work_minutes_per_week, wssd.varvar_max_work_minutes_per_week
from OpenJson((CAST(#pootsy_AWWC_request as nvarchar(max))),'$.data')
WITH (
id int,
attributes nvarchar(max) as json
) as WA
cross apply openjson ((CAST(WA.attributes as nvarchar(max))))
WITH (
worker_contracts nvarchar(max) as json
) as WC
cross apply OpenJson ((CAST(WC.worker_contracts as nvarchar(max))))
WITH (
start_date date,
end_date date,
signing_date date,
contract_variability varchar(20),
vehicle_type varchar(20),
addendum varchar(10),
work_hours_per_week decimal(4,2),
comment nvarchar(max),
social_secretary_identifier int,
social_secretary_specific_data nvarchar(max) as json,
roster_week nvarchar(max) as json
) AS WAWC
cross apply OpenJson (WAWC.social_secretary_specific_data)
WITH (
affiliate_id int,
worker_details_id int,
created_at varchar(40),
updated_at varchar(40),
affiliate_worker_id int,
comment nvarchar(max),
author_id int,
parent_id int,
operating_headquarter_id int,
reference_period varchar(10),
varvar_ref_period_avg_minutes_per_week decimal(4,2),
varvar_min_work_minutes_per_week decimal(4,2),
varvar_max_work_minutes_per_week decimal(4,2)
) AS WSSD