> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cockroachlabs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# GeoJSON

GeoJSON is a textual data format for representing spatial information.  It is based on [JavaScript Object Notation (JSON)](https://www.json.org).

GeoJSON can be used to represent the following spatial objects, which also have [Well Known Text (WKT)](/docs/stable/well-known-text.md) and [Well Known Binary (WKB)](/docs/stable/well-known-binary.md) representations:

* Point
* LineString
* Polygon
* MultiPoint
* MultiLineString
* MultiPolygon
* GeometryCollection

GeoJSON introduces the following additional concepts, which are not part of WKT or WKB:

* A "Feature" object that can contain a geometric shape and some additional properties that describe that shape.  This is useful, for example, when drawing maps on the internet in color, such as on [geojson.io](http://geojson.io).  For an example showing how to add color to a GeoJSON feature, [see below](#geojson-features-example).
* Features can additionally be grouped together into a "FeatureCollection".

> [!TIP]
>
> For more detailed information, see the [GeoJSON RFC](https://www.rfc-editor.org/rfc/rfc7946.txt).

> [!NOTE]
>
> GeoJSON should only be used for spatial data that uses the [WGS84](/docs/stable/architecture/glossary.md) geographic spatial reference system.  For more information, see [SRID 4326](/docs/stable/srid-4326.md).

## Example

In the example below, we will convert a shape represented in [Well Known Text](/docs/stable/well-known-text.md) to GeoJSON using the `ST_AsGeoJSON` [function](/docs/stable/functions-and-operators.md#spatial-functions).

Here is the WKT:

```
SRID=4326;POLYGON((-87.906471 43.038902, -95.992775 36.153980, -75.704722 36.076944, -87.906471 43.038902), (-87.623177 41.881832, -90.199402 38.627003, -82.446732 38.413651, -87.623177 41.881832))
```

Convert it to GeoJSON using the `ST_AsGeoJSON` function:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SELECT ST_AsGeoJSON('SRID=4326;POLYGON((-87.906471 43.038902, -95.992775 36.153980, -75.704722 36.076944, -87.906471 43.038902), (-87.623177 41.881832, -90.199402 38.627003, -82.446732 38.413651, -87.623177 41.881832))');
```

This is the JSON output of the above, but formatted:

```json theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
{
    "type": "Polygon",
    "coordinates": [
        [
            [
                -87.906471,
                43.038902
            ],
            [
                -95.992775,
                36.15398
            ],
            [
                -75.704722,
                36.076944
            ],
            [
                -87.906471,
                43.038902
            ]
        ],
        [
            [
                -87.623177,
                41.881832
            ],
            [
                -90.199402,
                38.627003
            ],
            [
                -82.446732,
                38.413651
            ],
            [
                -87.623177,
                41.881832
            ]
        ]
    ]
}
```

The JSON below is modified from the output above: it is grouped into a GeoJSON `FeatureCollection` in which each `Feature` has additional styling information (in the `properties` field) that can be used in visualization tools such as [geojson.io](http://geojson.io):

```json theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
{
    "type": "FeatureCollection",
    "features": [
        {
            "properties": {
                "fill-opacity": 0.3,
                "stroke": "#30D5C8",
                "stroke-width": 5,
                "fill": "#30D5C8"
            },
            "geometry": {
                "coordinates": [
                    [
                        [
                            -87.906471,
                            43.038902
                        ],
                        [
                            -95.992775,
                            36.15398
                        ],
                        [
                            -75.704722,
                            36.076944
                        ],
                        [
                            -87.906471,
                            43.038902
                        ]
                    ],
                    [
                        [
                            -87.623177,
                            41.881832
                        ],
                        [
                            -90.199402,
                            38.627003
                        ],
                        [
                            -82.446732,
                            38.413651
                        ],
                        [
                            -87.623177,
                            41.881832
                        ]
                    ]
                ],
                "type": "Polygon"
            },
            "type": "Feature"
        },
        {
            "properties": {
                "stroke": "yellow",
                "fill-opacity": 0.3,
                "stroke-width": 9,
                "fill": "yellow"
            },
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [
                        -87.623177,
                        41.881832
                    ],
                    [
                        -90.199402,
                        38.627003
                    ],
                    [
                        -82.446732,
                        38.413651
                    ],
                    [
                        -87.623177,
                        41.881832
                    ]
                ]
            },
            "type": "Feature"
        }
    ]
}
```

Here is the geometry described above as shown on [geojson.io](http://geojson.io):

![GeoJSON.io output](https://mintcdn.com/cockroachlabs/bBD0UEapbn6fk8Rb/images/common/geospatial/geojson_example.png?fit=max\&auto=format\&n=bBD0UEapbn6fk8Rb\&q=85\&s=5de05e31265cd46e65c7ab6cecfcded5)

## See also

* [GeoJSON RFC](https://www.rfc-editor.org/rfc/rfc7946.txt)
* [Spatial Data Overview](/docs/stable/spatial-data-overview.md)
* [Spatial indexes](/docs/stable/spatial-indexes.md)
* [Spatial and GIS Glossary of Terms](/docs/stable/architecture/glossary.md)
* [Well known text](/docs/stable/well-known-text.md)
* [Well known binary](/docs/stable/well-known-binary.md)
* [SRID 4326 - longitude and latitude](/docs/stable/srid-4326.md)
