Skip to content
Advertisement

Import JSON File into SQL Server Table with nested Arrays

I’m trying to import the Census Block GeoJSON file and unable to get “Coordinates” for each block along with it’s properties. I’m trying to get the ID, BlockGrp, Block.. and it’s associated coordinates. Below is my code, but I’m unable to get the coordinates since it’s in an array. Can anybody guide me on how to make it work?

The output is as follows:

The GeoJSON file structure is as follows:

The GEOJSON file is available here.

Advertisement

Answer

I think you are on the right track, you should just perform a few additional steps:

  • retrieve the content of coordinates property using the AS JSON syntax
  • add two additional OPENJSON statements to shred your array to the [Longitude,Latitude] level with CROSS APPLY statements
  • retrieve Longitude and Latitude values with JSON_VALUE in your SELECT statement.

This is a sample query that should extract what you need:

Sample output:

enter image description here

You can see this code in action on a subset of your data in this fiddle.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement