Hi Im having issues parsing part of a json file into sql server. I'm struggling to parse the phonenumbers object into 2 columns to identify the 2 phonenumber types: type_private and type_business . i want the dispplayed in 1 row not 2
so i want 1 row
| type_private | type_business|
|4444444444444 |8888888888888 |
not 2 rows
|type |number |
|private |4444444444444|
|business|8888888888888|
code is below but i cant get the "problem code " statements below to work. I would really appreciate any help . (Note Im new to json files and fairly new to sql server)
Thanks in advance K
DECLARE @jsonInfo NVARCHAR(MAX)
SELECT @jsoninfo =
N'{
"policyholders":
{
"details": {
"contactdetails":
{
"phonenumbers": [
{
"type": "private",
"number": "4444444444444"
},
{
"type": "business",
"number": "8888888888888"
}
]
}
}
}
}'
SELECT *
FROM OPENJSON (@jsoninfo, '$.policyholders' )
WITH ( details nvarchar(max) as json ) as policyholders_1
CROSS APPLY OPENJSON (policyholders_1.details)
WITH ( contactdetails nvarchar(max) as json ) as details_2
CROSS APPLY OPENJSON(details_2.contactdetails)
WITH (phonenumbers nvarchar(max) as json ) as contactdetails_3
--problem code start
cross apply ( select ( JSON_QUERY(@jsoninfo,'$.phonenumbers[0]') ) )
as phonenumbers_private_4 ([phonenumber_private_json])
cross apply ( select ( JSON_QUERY(@jsoninfo,'$.phonenumbers[1]') ) )
as phonenumbers_business_4 ([phonenumber_business_json])
--problem code end