Features To JSON (Conversion)

License Level:BasicStandardAdvanced

Summary

Converts features to JSON 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 extension.

Usage

Syntax

FeaturesToJSON_conversion (in_features, out_json_file, {format_json}, {include_z_values}, {include_m_values})
ParameterExplanationData Type
in_features

The features to convert to JSON.

Feature Layer
out_json_file

The output JSON file.

File
format_json
(Optional)

The JSON can be formatted to improve human readability similar to 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 human readability.
Boolean
include_z_values
(Optional)

Include Z value 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)

Include M value 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

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 Select layer by attribute and Select layer by location.

# 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", "lyr") 
 
# Select all cities which overlap the chihuahua polygon
arcpy.SelectLayerByLocation_management("lyr", "intersect", "chihuahua", 0, "new_selection")

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

Environments

Related Topics

Licensing Information

ArcGIS for Desktop Basic: Yes
ArcGIS for Desktop Standard: Yes
ArcGIS for Desktop Advanced: Yes
3/3/2014