Error converting Json file to a feature class

3376
7
Jump to solution
08-20-2018 06:55 AM
JustinBridwell2
Occasional Contributor II

I want to convert some json data being returned from a request to a feature class, but I keep getting a RuntimeError: Object: Error in executing tool. I made a test gdb in a folder 'C:/Workspace/Sandbox/ScratchTests/cslf.gdb' in which to populate my new feature class after conversion. To test that the request is correct and that I am returning Json data, I added a couple of print statements. Otherwise, everything is pretty straight forward. Does anyone see a problem with my code? I am following the arcpy [JSON to Features][1] directions from the documentation. The only thing I am doing differently is instead of using an actual file, I am just plugging in the variable 'cslfJson`.

import arcpy, sys, os, arcgis, requests 
arcpy
.env.workspace = "C:/Workspace/Sandbox/ScratchTests"
params
= {'f': 'json', 'where': '1=1', 'geometryType': 'esriGeometryPolygon', 'spatialRel': 'esriSpatialRelIntersects','outFields': '*', 'returnGeometry': 'false'}
r = requests.get('https://hazards.fema.gov/gis/nfhl/rest/services/CSLF/Prelim_CSLF/MapServer/3/query', params)
print(r.url)
cslfJson
= r.json()
print(cslfJson)
arcpy
.JSONToFeatures_conversion(cslfJson, os.path.join("cslf.gdb", "cslf"))
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Besides the missing geometry that Randy Burton‌ pointed out, your code isn't going to work because JSON To Features—Conversion toolbox | ArcGIS Desktop requires a "JSON file", not JSON text or some kind of JSON object.

Syntax

JSONToFeatures_conversion (in_json_file, out_features, {geometry_type})
ParameterExplanationData Type
in_json_file

The JSON file (.json) to convert.

File
out_features

The output feature class to create.

Feature Class
geometry_type
(Optional)

The geometry type to convert from GeoJSON to features. This option is only used when the input is a GeoJSON file. If the GeoJSON file does not contain any of the selected geometry type, the output feature class will be empty.

  • POINT Convert any points to features.
  • MULTIPOINT Convert any multipoints to features.
  • POLYLINE Convert any polylines to features.
  • POLYGON Convert any polygons to features.
String

It would be great if Esri support in-memory streams (e.g., io.SteamIO) in addition to files, but I am not holding my breath.  In the meantime, you have to dump the JSON results to a text file and then pass that file to the conversion tool.

I get a sense there is a more straightforward way of going about this using the ArcGIS API for Python, but I would need some more information before suggesting anything specific.

View solution in original post

7 Replies
RandyBurton
MVP Regular Contributor

Can you provide the complete error message?

JustinBridwell2
Occasional Contributor II

Here's the traceback:

Traceback (most recent call last):

File "<ipython-input-21-7d5d88079864>", line 1, in <module>
runfile('C:/Workspace/Sandbox/ScratchTests/CSLF.py', wdir='C:/Workspace/Sandbox/ScratchTests')

File "C:\Users\jbridwell\AppData\Local\Continuum\anaconda3\envs\acrpro\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile
execfile(filename, namespace)

File "C:\Users\jbridwell\AppData\Local\Continuum\anaconda3\envs\acrpro\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Workspace/Sandbox/ScratchTests/CSLF.py", line 20, in <module>
arcpy.JSONToFeatures_conversion(cslfJson, os.path.join("cslfalso.gdb", "cslf"))

File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\conversion.py", line 403, in JSONToFeatures
raise e

File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\conversion.py", line 400, in JSONToFeatures
retval = convertArcObjectToPythonObject(gp.JSONToFeatures_conversion(*gp_fixargs((in_json_file, out_features), True)))

File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 506, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))

RuntimeError: Object: Error in executing tool

0 Kudos
RandyBurton
MVP Regular Contributor

And when you print(cslfJson), does it appear to be correctly formatted json and a feature?

JustinBridwell2
Occasional Contributor II

Yep. You can actually try it yourself with the code above. Thats a public Map service from fema. Just import requests, json and the rest of the code (except arcpy.env.workspace) down to print(cslf). It should work the same for you as me.

0 Kudos
RandyBurton
MVP Regular Contributor

I hit a timeout on my data request, so there are a lot of records; the json indicated: "exceededTransferLimit": true with perhaps 1,000 records returned.  You may need to do something to limit your query results.

I also noticed that you are not requesting the geometry ( 'returnGeometry': 'false' )  which would cause JSONToFeatures to fail.  You should see something like this in your json:

   "geometry": {
    "rings": [
     [
      [
       -9165686.7350331135,
       3936948.9098836873
      ],
      [
       -9165681.1218573805,
       3936946.988356465
      ],
      [
       -9165681.5995294657,
       3936947.138881634
      ],
      [
       -9165686.7350331135,
       3936948.9098836873
      ]
     ]
    ]
   }
JoshuaBixby
MVP Esteemed Contributor

Besides the missing geometry that Randy Burton‌ pointed out, your code isn't going to work because JSON To Features—Conversion toolbox | ArcGIS Desktop requires a "JSON file", not JSON text or some kind of JSON object.

Syntax

JSONToFeatures_conversion (in_json_file, out_features, {geometry_type})
ParameterExplanationData Type
in_json_file

The JSON file (.json) to convert.

File
out_features

The output feature class to create.

Feature Class
geometry_type
(Optional)

The geometry type to convert from GeoJSON to features. This option is only used when the input is a GeoJSON file. If the GeoJSON file does not contain any of the selected geometry type, the output feature class will be empty.

  • POINT Convert any points to features.
  • MULTIPOINT Convert any multipoints to features.
  • POLYLINE Convert any polylines to features.
  • POLYGON Convert any polygons to features.
String

It would be great if Esri support in-memory streams (e.g., io.SteamIO) in addition to files, but I am not holding my breath.  In the meantime, you have to dump the JSON results to a text file and then pass that file to the conversion tool.

I get a sense there is a more straightforward way of going about this using the ArcGIS API for Python, but I would need some more information before suggesting anything specific.

View solution in original post

Juan_JulioLiñan_rebaza
New Contributor II

Hi Justin, as Joshua pointed out, try to have the json file before run the tool. May be this could help you.

import arcpy, sys, os, arcgis, requests, json
arcpy.env.workspace = "C:/Workspace/Sandbox/ScratchTests"
#Create a route to store your json file
jsonFile="C:/Workspace/Sandbox/ScratchTests/jsonTest.json"

params = {'f': 'json', 'where': '1=1', 'geometryType': 'esriGeometryPolygon', 'spatialRel': 'esriSpatialRelIntersects','outFields': '*', 'returnGeometry': 'false'}
r = requests.get('https://hazards.fema.gov/gis/nfhl/rest/services/CSLF/Prelim_CSLF/MapServer/3/query', params)
print(r.url)
cslfJson = r.json()
print(cslfJson)
#Pass the request to a jsonfile
with open(jsonFile,'w') as jsonDir:
json.dump(cslfJson,jsonDir)

arcpy.JSONToFeatures_conversion(jsonFile, os.path.join("cslf.gdb", "cslf"))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍