Probably a poor title. apologies. I'm working with a large set of json data with nested arrays that are often without a name. I'm trying to extract part of this data and then just access it by array offset.
Example that returns the array data as rows
DECLARE @json NVARCHAR(MAX)
SET @json =
N'[
{ "id" : 2,"info": { "name": "John", "surname": "Smith" }, "age": 25 },
{ "id" : 5,"info": { "name": "Jane", "surname": "Smith", "skills": [["SQL", "C#", "Azure"],["ABC","DEF","BLASH"]] }, "dob": "2005-11-04T12:00:00" }
]'
SELECT arr
FROM OPENJSON(@json)
WITH (id int 'strict $.id',
firstName nvarchar(50) '$.info.name', lastName nvarchar(50) '$.info.surname',
age int, dateOfBirth datetime2 '$.dob',
skillarr nvarchar(max) '$.info.skills' as json)
cross apply openjson( skillarr ) with ( arr nvarchar(MAX) '$' AS JSON )
this will return ["SQL", "C#", "Azure"] ["ABC","DEF","BLASH"]
but I would then like to return only the first elements of each array subset. i.e "SQL" "ABC"
I have tried a second cross apply but cannot figure out the path convention
cross apply openjson (arr ) with (firstskill nvarchar(10) '$.[0]')
is not valid. Anyone know how I just access by index on unnamed data?
Thanks