ArcGIS Pro 3.0 API Reference Guide
ArcGIS.Core.Geometry Namespace / GeometryEngine Class / ExportToJson Method
A combination of JsonExportFlags which determines how the geometry is exported.
The input geometry to write to a string.
Example

In This Topic
    ExportToJson Method (GeometryEngine)
    In This Topic
    Writes a JSON version of the input geometry to a string.
    Syntax
    Public Function ExportToJson( _
       ByVal exportFlags As JsonExportFlags, _
       ByVal geometry As Geometry _
    ) As String

    Parameters

    exportFlags
    A combination of JsonExportFlags which determines how the geometry is exported.
    geometry
    The input geometry to write to a string.

    Return Value

    The JSON version of the input geometry.
    Exceptions
    ExceptionDescription
    Geometry is null or empty.
    The method is not implemented for GeometryBag.
    Remarks
    Note that if the geometry contains ID information, this property will not be preserved in the JSON string. ID is not part of the JSON specification.
    Example
    Import and Export Geometries to JSON
    // MapPoint
    string inputString = 
      "{\"x\":1,\"y\":2,\"spatialReference\":{\"wkid\":4326,\"latestWkid\":4326}}";
    Geometry geometry = GeometryEngine.Instance.ImportFromJson(
      JsonImportFlags.JsonImportDefaults, inputString);
    
    MapPoint importPoint = geometry as MapPoint;
    // importPoint = 1, 2
    // importPoint.SpatialReference.WKid = 4326
    
    // use the MapPointBuilderEx convenience method
    MapPoint importPoint2 = MapPointBuilderEx.FromJson(inputString);
    // importPoint2 = 1, 2
    // impointPoint2.SpatialReference.Wkid = 4326
    
    string outputString = GeometryEngine.Instance.ExportToJson(
      JsonExportFlags.JsonExportDefaults, importPoint);
    // outputString =
    //   "{\"x\":1,\"y\":2,\"spatialReference\":{\"wkid\":4326,\"latestWkid\":4326}}"
    
    string outputString2 = importPoint.ToJson();
    
    inputString = 
      "{\"spatialReference\":{\"wkid\":4326},\"z\":3,\"m\":4,\"x\":1,\"y\":2}";
    importPoint = GeometryEngine.Instance.ImportFromJson(
      JsonImportFlags.JsonImportDefaults, inputString) as MapPoint;
    // importPoint.HasM = true
    // importPoint.HasZ = true
    // importPoint.X = 1
    // importPoint.Y = 2
    // importPoint.M = 4
    // importPoint.Z = 3
    
    importPoint2 = MapPointBuilderEx.FromJson(inputString);
    
    // export to json - skip spatial reference
    outputString = GeometryEngine.Instance.ExportToJson(
                  JsonExportFlags.JsonExportSkipCRS, importPoint);
    // outputString = "{\"x\":1,\"y\":2,\"z\":3,\"m\":4}"
    
    // export from mappoint, skipping the sr - same as GeometryEngine.Instance.ExportToJson w JsonExportFlags.JsonExportSkipCRS
    outputString2 = importPoint.ToJson(true);
    
    //
    // Multipoint
    //
    List<Coordinate2D> coords = new List<Coordinate2D>()
    {
      new Coordinate2D(100, 200),
      new Coordinate2D(201, 300),
      new Coordinate2D(301, 400),
      new Coordinate2D(401, 500)
    };
    
    Multipoint multipoint = MultipointBuilderEx.CreateMultipoint(
      coords, SpatialReferences.WebMercator);
    
    inputString = 
      "{\"points\":[[100,200],[201,300],[301,400],[401,500]],\"spatialReference\":{\"wkid\":3857}}";
    Multipoint importMultipoint = 
      GeometryEngine.Instance.ImportFromJson(
             JsonImportFlags.JsonImportDefaults, inputString) as Multipoint;
    // importMultipoint.IsEqual(multipoint) = true
    
    ReadOnlyPointCollection points = importMultipoint.Points;
    // points.Count = 4
    // points[0] = 100, 200
    // points[1] = 201, 300
    // points[2] = 301, 400
    // points[3] = 401, 500
    
    // use the MultipointbuilderEx convenience method
    Multipoint importMultipoint2 = MultipointBuilderEx.FromJson(inputString);
    // importMultipoint2.IsEqual(multipoint) = true
    
    // export to json
    outputString = GeometryEngine.Instance.ExportToJson(
                    JsonExportFlags.JsonExportDefaults, multipoint);
    // outputString = inputString
    
    // or use the multipoint itself
    outputString2 = multipoint.ToJson();
    
    //
    // Polyline
    //
    Polyline polyline = PolylineBuilderEx.CreatePolyline(
                             coords, SpatialReferences.WebMercator);
    
    // export without the spatial reference
    outputString = GeometryEngine.Instance.ExportToJson(
                            JsonExportFlags.JsonExportSkipCRS, polyline);
    // import
    geometry = GeometryEngine.Instance.ImportFromJson(
                 JsonImportFlags.JsonImportDefaults, outputString);
    Polyline importPolyline = geometry as Polyline;
    // importPolyline.SpatialReference = null
    
    
    points = importPolyline.Points;
    // points.Count = 4
    // points[0] = 100, 200
    // points[1] = 201, 300
    // points[2] = 301, 400
    // points[3] = 401, 500
    
    // use the polylineBuilderEx convenience method 
    Polyline importPolyline2 = PolylineBuilderEx.FromJson(outputString);
    // importPolyline2 = importPolyline
    
    outputString2 = importPolyline2.ToJson();
    // outputString2 = outputString
    
    //
    // Polygon
    //
    Polygon polygon = PolygonBuilderEx.CreatePolygon(
                                  coords, SpatialReferences.WebMercator);
    
    // export without the spatial reference
    outputString = GeometryEngine.Instance.ExportToJson(
                              JsonExportFlags.JsonExportSkipCRS, polygon);
    // import
    geometry = GeometryEngine.Instance.ImportFromJson(
                         JsonImportFlags.JsonImportDefaults, outputString);
    
    Polygon importPolygon = geometry as Polygon;
    // importPolygon.SpatialReference = null
    points = importPolygon.Points;
    // points.Count = 5
    
    // polygonBuilderEx convenience method
    Polygon importPolyon2 = PolygonBuilderEx.FromJson(outputString);
    // importPolygon2 = importPolygon
    
    // export from the polygon
    outputString2 = importPolyon2.ToJson(true);
    
    // Empty polygon
    polygon = PolygonBuilderEx.CreatePolygon(SpatialReferences.WebMercator);
    outputString = GeometryEngine.Instance.ExportToJson(
                                   JsonExportFlags.JsonExportDefaults, polygon);
    importPolygon = GeometryEngine.Instance.ImportFromJson(
                        JsonImportFlags.JsonImportDefaults, outputString) as Polygon;
    
    // importPolygon.IsEmpty = true
    // importPolygon.SpatialReference.Wkid = 3857
    
    Requirements

    Target Platforms: Windows 11, Windows 10, Windows 8.1

    See Also