I wonder how can I put this JSON object, consisting of two float numbers arrays, into SQL Database Table:
{
"Latitudes": [
53.665084,
53.665084,
53.665184,
53.665284
],
"Longitudes":[
17.342853,
17.342953,
17.343053,
17.343153
]
}
Table has 3 columns: CoordID, Latitude, Longitude (CoordID is auto-incremented). I would like to put each pair of those tables ( [0],[0] / [1][1] etc. ) as one row in table. So with this object, it would be 4 rows.
Here is the code which doesn't work as I expect - it just puts nulls in place of "Longitude" and "Latitude" columns and it only creates ONE ROW... (Instead of 4 rows):
declare @json NVARCHAR(MAX);
SET @json=
'{
"Latitudes": [
53.665084,
53.665084,
53.665184,
53.665284
],
"Longitudes":[
17.342853,
17.342953,
17.343053,
17.343153
]
}'
insert into dbo.Coords(
[Latitude],[Longitude])
select Latitude, Longitude from OPENJSON(@json)
WITH(
Latitude FLOAT '$."Latitudes"',
Longitude FLOAT '$."Longitudes"')
P.S OPENJSON() function works from version 2016 of SQL Server or on the Azure SQL Database. Just in case if anyone would try to run this in SSMS 2014 i.e ;-)