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
FeaturesToJSON(in_features, out_json_file, {format_json}, {include_z_values}, {include_m_values}, {geoJSON}, {outputToWGS84}, {use_field_alias})
Parameter | Explanation | Data 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.
| Boolean |
include_z_values (Optional) | Specifies whether to include z-values of the features to the JSON.
| Boolean |
include_m_values (Optional) | Specifies whether to include m-values of the features to the JSON.
| Boolean |
geoJSON (Optional) | Specifies whether the output will be created as GeoJSON, conforming to the GeoJSON specification.
| 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.
| Boolean |
use_field_alias (Optional) | Specifies whether the output file will use field aliases for feature attributes.
| Boolean |
Code sample
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")
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")
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")
Environments
Licensing information
- Basic: Yes
- Standard: Yes
- Advanced: Yes