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
Related
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
I have a table structure as below in SQL Server database,
I want to populate the data from database something similar to JSON like as below:
id: 1
aname: xyz
categories: bus
{
arnam: res
street: [s1,s2]
},
{
arnam: com
street: [c1,c2]
}
Can someone please guide me as to how I can do this in the database using normal SQL query or procedure.
Your json is not valid, but according to your table I think you want to know how to parse data from nested JSON with array of values with this structure:
WITH cte AS (
SELECT * FROM (VALUES
('{"id": 1, "aname": "xyz",
"categories": {
"bus": [
{"aname": "res",
"street": ["c1", "c2"]
},
{"aname": "res",
"street": ["s1", "s2"]
}]
}
}'),
('{"id": 2, "aname": "abc",
"categories": {
"bus": [
{"aname": "foo",
"street": ["c1", "c2"]
},
{"aname": "zoo",
"street": ["s1", "s2"]
}]
}
}')
) t1 ([json])
)SELECT
ROW_NUMBER() OVER(ORDER BY [id]) AS RN,
*
FROM cte AS e
CROSS APPLY OPENJSON(e.[json]) WITH (
[id] int '$.id',
[aname] VARCHAR(100) '$.aname',
[categories_jsn] NVARCHAR(MAX) '$.categories.bus' AS JSON
) AS jsn
CROSS APPLY OPENJSON([categories_jsn]) WITH (
[street_arr] NVARCHAR(MAX) '$.street' AS JSON,
[aname_lvl2] VARCHAR(20) '$.aname'
) AS jsn2
CROSS APPLY OPENJSON([street_arr]) WITH (
[street] VARCHAR(20) '$'
)
Output:
I am trying to extract a specific value from a JSON column in SQL Server. Unfortunately I have read several posts on this topic but still cannot figure out how to translate their solutions to what I need. I am looking to extract "foo testing" but simply do not understand how to get at this with a nested JSON. Can someone please advise?
The structure of the JSON column is:
{
"values": [
{
"id": "x01",
"status": "STATUS1",
"subStatus": "SubStatus1",
"values": [
{
"key": "dropdown",
"value": "",
"optionType": null
}
]
},
...
{
"id": "x03",
"status": "STATUS3",
"subStatus": "SubStatus3",
"values": [
{
"key": "dropdown",
"value": "",
"optionType": null
},
{
"key": "textInput",
"value": null,
"optionType": null
},
{
"key": "checkbox1",
"value": true,
"optionType": null
},
{
"key": "textInput2",
"value": "foo testing",
"optionType": null
}
]
}
]
}
The statement depends on the structure of the parsed JSON, in your case you need to use two nested OPENJSON() calls and additinal APPLY operators. Note, that you need to use AS JSON in a "values" column definition to specify that the referenced property contains an inner JSON array and the type of that column must be nvarchar(max).
Test table:
DECLARE #json varchar(max) = '
{
"values": [
{
"id": "x01",
"status": "STATUS1",
"subStatus": "SubStatus1",
"values": [
{"key": "dropdown", "value": "", "optionType": null}
]
},
{
"id": "x03",
"status": "STATUS3",
"subStatus": "SubStatus3",
"values": [
{"key": "dropdown", "value": "", "optionType": null},
{"key": "textInput", "value": null, "optionType": null},
{"key": "checkbox1", "value": true, "optionType": null},
{"key": "textInput2", "value": "foo testing", "optionType": null}
]
}
]
}
'
SELECT JsonColumn
INTO JsonTable
FROM (VALUES (#json)) v (JsonColumn)
Statement:
SELECT j1.[id], j2.[key], j2.[value] -- or add all columns
FROM JsonTable t
CROSS APPLY OPENJSON(t.JsonColumn, '$.values') WITH (
[id] varchar(3) '$.id',
[status] varchar(30) '$.status',
[subStatus] varchar(30) '$.subStatus',
[values] nvarchar(max) '$.values' AS JSON
) j1
CROSS APPLY OPENJSON(j1.[values], '$') WITH (
[key] varchar(50) '$.key',
[value] varchar(50) '$.value',
[optionType] varchar(50) '$.optionType'
) j2
Result:
id key value
---------------------------
x01 dropdown
x03 dropdown
x03 textInput
x03 checkbox1 true
x03 textInput2 foo testing
you can use following query
;with summery as(
SELECT *
FROM OPENJSON((SELECT value FROM OPENJSON(#json)))
WITH (
id NVARCHAR(50) 'strict $.id',
status NVARCHAR(50) '$.status',
subStatus NVARCHAR(50) '$.subStatus',
[values] NVARCHAR(max) '$.values' AS JSON
)
)
select id,status,subStatus,[key],value,optionType from summery
CROSS APPLY OPENJSON(summery.[values])
WITH (
[key] NVARCHAR(50) '$.key',
[value] NVARCHAR(50) '$.value',
[optionType] NVARCHAR(50) '$.optionType'
);
demo in db<>fiddle
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;
How can we use SQL to Convert a JSON statement into different tables?
For example we have JSON:
{"table1":
{"Name":"table1","Items":
[{"Id":1,"FirstName":"John",
"LastName":"Wen","Country":"UK",
"PostCode":1234,"Status":false,
"Date":"2018-09-18T08:30:32.91",}]},
"table2":
{"Name":"table2","Items":
[{"Id":1,"Name":"leo",
"StudentId":102,"CreatedDate":"2018-09-18","Location":"USA"}]}}
In the relational database, we will get two tables once the JSON is converted
For example, schema 'Table1':
Id FirstName LastName Country PostCode Status Date
1 John Wen UK 1234 false 2018-09-18T08:30:32.91
And the 'Table2' will look like:
Id Name StudentId CreateDate Location
1 Leo 102 2018-9-18 USA
Could anyone please give any advices on it.
You can do this using openjson and json_value functions. Try the following:
Declare #json nvarchar(max),#table1Items nvarchar(max), #table2Items nvarchar(max)
set #json='{
"table1": {
"Name": "table1",
"Items": [{
"Id": 1,
"FirstName": "John",
"LastName": "Wen",
"Country": "UK",
"PostCode": 1234,
"Status": false,
"Date": "2018-09-18T08:30:32.91"
}, {
"Id": 2,
"FirstName": "John1",
"LastName": "Wen1",
"Country": "UK1",
"PostCode": 12341,
"Status": true,
"Date": "2018-09-15T08:30:32.91"
}]
},
"table2": {
"Name": "table2",
"Items": [{
"Id": 1,
"Name": "leo",
"StudentId": 102,
"CreatedDate": "2018-09-18",
"Location": "USA"
}]
}
}'
set #table1Items=(select value from OpenJSON((select value from OpenJSON(#Json) where [key]='table1')) where [key]='Items')
set #table2Items=(select value from OpenJSON((select value from OpenJSON(#Json) where [key]='table2')) where [key]='Items')
--select for table 1
select JSON_VALUE(val,'$.Id') as ID,
JSON_VALUE(val,'$.FirstName') as FirstName,
JSON_VALUE(val,'$.LastName') as LastName,
JSON_VALUE(val,'$.Country') as Country,
JSON_VALUE(val,'$.PostCode') as PostCode,
JSON_VALUE(val,'$.Status') as Status,
JSON_VALUE(val,'$.Date') as Date
from
(
select value as val from openJSON(#table1Items)
) AS Table1JSON
--select for table 1
select JSON_VALUE(val,'$.Id') as ID,
JSON_VALUE(val,'$.Name') as FirstName,
JSON_VALUE(val,'$.StudentId') as LastName,
JSON_VALUE(val,'$.CreatedDate') as Country,
JSON_VALUE(val,'$.Location') as PostCode
from
(
select value as val from openJSON(#table2Items)
) AS Table2JSON
It is working exactly as you wanted. At the end, the two select statements return the tables as you mentioned. Just add them to your desired table using insert into select. I have also tried adding another object to table1 array and verified that it is working fine i.e. returning two rows for two objects. Hope this helps
If SQL Version 2016+ use OPENJSON AND with_clause:
https://learn.microsoft.com/en-us/sql/t-sql/functions/openjson-transact-sql?view=sql-server-2017
DECLARE #JsonData NVARCHAR(MAX);
SET #JsonData = N'
{
"table1": {
"Name": "table1",
"Items": [
{
"Id": 1,
"FirstName": "John",
"LastName": "Wen",
"Country": "UK",
"PostCode": 1234,
"Status": false,
"Date": "2018-09-18T08:30:32.91"
},
{
"Id": 2,
"FirstName": "John1",
"LastName": "Wen1",
"Country": "UK1",
"PostCode": 12341,
"Status": true,
"Date": "2018-09-15T08:30:32.91"
}
]
},
"table2": {
"Name": "table2",
"Items": [
{
"Id": 1,
"Name": "leo",
"StudentId": 102,
"CreatedDate": "2018-09-18",
"Location": "USA"
}
]
}
}
';
--Table1
SELECT [a].[Id]
, [a].[FistName]
, [a].[Lastname]
, [a].[Country]
, [a].[PostCode]
, [a].[Status]
, [a].[Date]
FROM
OPENJSON(#JsonData, '$.table1.Items')
WITH (
[Id] INT '$.Id'
, [FistName] NVARCHAR(200) '$.FirstName'
, [Lastname] NVARCHAR(200) '$.LastName'
, [Country] NVARCHAR(200) '$.Country'
, [PostCode] NVARCHAR(200) '$.PostCode'
, [Status] NVARCHAR(200) '$.Status'
, [Date] DATETIME '$.Date'
) [a];
--Table2
SELECT [a].[Id]
, [a].[Name]
, [a].[StudentId]
, [a].[CreatedDate]
, [a].[Location]
FROM
OPENJSON(#JsonData, '$.table2.Items')
WITH (
[Id] INT '$.Id'
, [Name] NVARCHAR(200) '$.Name'
, [StudentId] INT '$.StudentId'
, [CreatedDate] DATETIME '$.CreatedDate'
, [Location] NVARCHAR(200) '$.Location'
) [a];
Try using OPENJSON function in SQL Server:
ReferenceLink_1
ReferenceLink_2