GeoJSON is popular format for spatial data representation. If you receive text formatted as GeoJSON from other systems, you can load it into SQL Server and convert it into spatial types.
New OPENJSON function in SQL Server 2016 enables you to parse and load GeoJSON text into SQL Server spatial types.
In this example, I will load GeoJSON text that contains a set of bike share locations in Washington DC. GeoJSON sample is provided ESRI and it can be found in https://github.com/Esri/geojson-layer-js/blob/master/data/dc-bike-share.json
I will load this GeoJSON text using following query:
I have created a table BikeShare that will contain spatial data and I have defined local text variable @bikeShares that contains GeoJSON taken from https://github.com/Esri/geojson-layer-js/blob/master/data/dc-bike-share.json .
Then I will open GeoJSON rowset from @bikeShares variable using OPENJSON function. OPENJSON will return the one row for each object in GeoJSON array with the schema defined in WITH clause:
Values in long and lat are used to initialize spatial type using geography::STGeomFromText method, and other values are inserted in table columns.
When I execute this script, I can query spatial data loaded from GeoJSON text:
select position.STAsText(), ObjectId, Address, Bikes, Docks
FROM BikeShare
If you try to execute the same script you will see that all objects from GeoJSON variable are not in the table.
OPENJSON function enables you to parse any JSON text. You can use this function to convert GeoJSON format into SQL Server spatial types.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.