Features To JSON (Conversion)

Summary

Converts features to JSON or GeoJSON format. The fields, geometry, and spatial reference of features will be converted to their corresponding JSON representation and written to a file with a .json or .geojson extension.

Usage

  • If you want the JSON representation in the output file to be more readable, you can enable formatting. The output file will be formatted with spaces, tabs, and carriage returns to improve readability. A formatted JSON may be beneficial for application development and testing. However, it is not recommended for production applications since all of the whitespace is unnecessary and ignored by JSON parsers. Additionally, formatted JSON can be significantly larger than its JSON equivalent, and the file size will be larger than its corresponding JSON representation. This can affect application performance.

  • The conversion does not support joins, relates, and attachments of the features.

  • To convert a subset of features in a feature class or layer, use the Select Layer By Attribute or Select Layer By Location tool and select the subset of features to be converted before using Features To JSON.

  • Use the Output to GeoJSON parameter (geoJSON='GEOJSON' in Python) to create a .geojson file output that conforms to the GeoJSON specification. Esri JSON output is the default.

  • When the output is a GeoJSON file, you have the option to project the input features to the WGS 1984 coordinate system, which is the standard for the GeoJSON specification. A default geographic transformation will be applied if necessary. If this option is not used, the output GeoJSON file will contain a crs tag that can be used in some applications to define the coordinate system or coordinate reference system. This tag is not fully supported under the GeoJSON specification.

  • The tool's output will only include visible attribute fields from the input. To include all fields, you must make them all visible. Additionally, you can use the Use field aliases parameter to make the output use field aliases rather than field names.

Syntax

arcpy.conversion.FeaturesToJSON(in_features, out_json_file, {format_json}, {include_z_values}, {include_m_values}, {geoJSON}, {outputToWGS84}, {use_field_alias})
ParameterExplanationData Type
in_features

The features to convert to JSON.

Feature Layer
out_json_file

The output JSON or GeoJSON file.

File
format_json
(Optional)

Specifies whether the JSON will be formatted to improve readability similar to the ArcGIS REST API specification's PJSON (Pretty JSON) format.

  • NOT_FORMATTED The features will not be formatted. This is the default.
  • FORMATTEDThe features will be formatted to improve readability.
Boolean
include_z_values
(Optional)

Specifies whether to include z-values of the features to the JSON.

  • NO_Z_VALUES The z-values will not be included in geometries, and the hasZ property of the JSON will not be included. This is the default.
  • Z_VALUESThe z-values will be included in geometries, and the hasZ property of the JSON will be set to true.
Boolean
include_m_values
(Optional)

Specifies whether to include m-values of the features to the JSON.

  • NO_M_VALUES The m-values will not be included in geometries, and the hasM property of the JSON will not be included. This is the default.
  • M_VALUESThe m-values will be included in geometries, and the hasM property of the JSON will be set to true.
Boolean
geoJSON
(Optional)

Specifies whether the output will be created as GeoJSON, conforming to the GeoJSON specification.

  • GEOJSON The output will be created in the GeoJSON format (.geojson).
  • NO_GEOJSONThe output will be created as Esri JSON (.json). This is the default.
Boolean
outputToWGS84
(Optional)

Specifies whether the input features will be projected to the geographic coordinate system WGS_1984 with a default geographic transformation. This parameter only applies when the output is GeoJSON.

  • WGS84 Features will be projected to WGS_1984.
  • KEEP_INPUT_SRFeatures will not be projected to WGS_1984. The GeoJSON will contain a CRS tag that defines the coordinate system. This is the default.
Boolean
use_field_alias
(Optional)

Specifies whether the output file will use field aliases for feature attributes.

  • USE_FIELD_NAME Output feature attributes will use field names. This is the default.
  • USE_FIELD_ALIASOutput feature attributes will use field aliases.
Boolean

Code sample

FeaturesToJSON example 1 (Python window)

The following Python window script demonstrates how to use the FeaturesToJSON function to create JSON and PJSON files.

import arcpy
import os

arcpy.env.workspace = "c:/data"
arcpy.FeaturesToJSON_conversion(os.path.join("outgdb.gdb", "myfeatures"), 
                                "myjsonfeatures.json")
arcpy.FeaturesToJSON_conversion(os.path.join("outgdb.gdb", "myfeatures"), 
                                "mypjsonfeatures.json", "FORMATTED")
FeaturesToJSON example 2 (Python window)

The following Python window script demonstrates how to use the FeaturesToJSON function with z- and m-values.

import arcpy
import os

arcpy.env.workspace = "c:/data"
arcpy.FeaturesToJSON_conversion(os.path.join("outgdb.gdb", "myfeatures"), 
                                "myjsonfeatures.json", "NOT_FORMATTED",	
                                "Z_VALUES", "M_VALUES")
FeaturesToJSON example 3 (stand-alone script)

Convert a subset of features to JSON using SelectLayerByAttribute and SelectLayerByLocation.

# Import system modules
import arcpy

# Set the workspace
arcpy.env.workspace = "c:/data/mexico.gdb"

# Make a layer from the feature class
arcpy.MakeFeatureLayer_management("cities", "cities_lyr") 
 
# Select all cities that overlap the chihuahua polygon
arcpy.SelectLayerByLocation_management("cities_lyr", "intersect", "chihuahua", 
                                       0, "new_selection")

# Within selected features, further select only those cities that have a 
# population > 10,000   
arcpy.SelectLayerByAttribute_management("cities_lyr", "SUBSET_SELECTION", 
                                        '"population" > 10000')
 
# Convert the selected features to JSON
arcpy.FeaturesToJSON_conversion("cities_lyr", r"c:\data\myjsonfeatures.json")

Licensing information

  • Basic: Yes
  • Standard: Yes
  • Advanced: Yes

Related topics