Skip to main content

Importing GeoJSON with a deeply nested object array

“Importing GeoJSON with a deeply nested object array“

Question

How do I import GeoJSON with a nested object array?

Answer

For this tutorial, we will use open data publicly available here. A copy can be found here.

  1. Download the data in GeoJSON format and rename the file to geojson.json.

  2. Understand the structure.

  1. Create a table to store the GeoJSON rows.

The requirement here is to generate a row for each object in the features array. The data type inferred for the field geometry suggests that it translates to ClickHouse's MultiPolygon data type.

  1. Prepare the data.

The main purpose of the query is to verify that we obtain one row for each object in the features array.

Note

The field features.geometry.coordinates is commented to make the result set more readable.

  1. Insert the data.

Here, we get the following error:

This is caused by the parsing of features.geometry.coordinates.

  1. Let's check its data type.

It can be fixed by casting multipolygon.properties.coordinates to Array(Array(Array(Tuple(Float64,Float64)))). To do so, we can use the function arrayMap(func,arr1,...).

  1. Insert the data.

Conclusion

Handling JSON can result in a complex task. This tutorial addressed a scenario where a nested object array could make this task even more difficult.
For any other JSON-related requirements, please refer to our documentation.

· 5 min read