Returns the GeoJSON representation of a geometry or geography object.
Syntax
text ST_AsGeoJSON(geometry geom, integer maxdecimaldigits, integer options);
text ST_AsGeoJSON(geography geog, integer maxdecimaldigits, integer options);
text ST_AsGeoJSON(integer gjVersion, geometry geom, integer maxdecimaldigits, integer options);
text ST_AsGeoJSON(integer gjVersion, geography geog, integer maxdecimaldigits, integer options);Parameters
| Parameter | Description |
|---|---|
geom | The geometry object to convert. |
geog | The geography object to convert. |
maxdecimaldigits | The maximum number of decimal places to retain in the output coordinates. Default value: 15. |
options | Additional members to include in the GeoJSON output. Valid values: 0 (default, no additional members), 1 (include the bbox bounding box member), 2 (include the coordinate reference system (CRS) using a short expression, such as EPSG:4326), 4 (include the CRS using a long expression, such as urn:ogc:def:crs:EPSG::4326). |
gjVersion | The version of the GeoJSON specification. If specified, set this to 1. |
Usage notes
ST_AsGeoJSON preserves Z coordinates for 3D geometries and does not drop the Z coordinate of the input geometry.
Examples
Convert a LineString geometry to its GeoJSON representation:
SELECT ST_AsGeoJSON('LINESTRING(1 2, 3 4)');Output:
{
"type":"LineString",
"coordinates":[[1,2],[3,4]]
}(1 row)