This topic describes the ST_AsGeoJSON function. This function returns the GeoJSON element that represents a geometry object or a 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 whose GeoJSON representation you want to obtain.
maxdecimaldigits The maximum number of decimal places that you want to retain. Default value: 15.
options The additional information that you want to return in the GeoJSON element. Valid values:
  • 0: specifies not to return additional information. This is the default value.
  • 1: specifies to return the bbox member of the GeoJSON element.
  • 2: specifies to return the coordinate reference system (CRS) of the GeoJSON element by using a short expression. An example expression is EPSG:4326.
  • 4: specifies to return the CRS of the GeoJSON element by using a long expression. An example expression is urn:ogc:def:crs:EPSG::4326.
geog The geography object whose GeoJSON representation you want to obtain.
gjVersion The version of the GeoJSON specification. If you specify this parameter, set the value to 1.

Description

This function supports 3D objects and does not discard the z-index of the geometry object that is constructed.

Examples

Obtain the GeoJSON element that represents a geometry object.
SELECT ST_AsGeoJSON('LINESTRING(1 2, 3 4)');
                   st_asgeojson
---------------------------------------------------
 {
     "type":"LineString",
  "coordinates":[[1,2],[3,4]]
 }
(1 row)