Search code examples
pythonjsonarcgis

GEOJSON to features (in ArcGIS) missing properties


I have a GEOJSON file that I am using to create a feature class in ArcGIS. My JSON has some missing properties and I've noticed that ArcGIS's JSONToFeatures tool adds missing properties from adjacent objects.

For example, my raw json looks like this (note the second feature is missing the 'site_id' property):

     {"type": "Feature", "geometry": 
           {"type": "Polygon", "coordinates": [[[-xxx.xxxx, xx.xxxxxxx],....]]]}, "properties": 
                {"id": "0", "address": "xx xxxxx", "site_id": "111111111"}},
     {"type": "Feature", "geometry": 
           {"type": "Polygon", "coordinates": [[[-xxx.xxxx, xx.xxxxxxx],....]]]}, "properties": 
                {"id": "1", "address": "xx xxxxx"}},

Processing this json with the Arc JSONToFeatures tool produces a table that looks like this:

OBJECTID ID Address site_id
0 xx xxx 1111111
1 xx xxx 1111111

NOTE: the site_id from the first record is added to the second record.

I'm using Python's requests and json modules to download and prepare the JSON file prior to creating the feature class. The JSON has 100,000 objects and this is a relatively regular process, not a one off task.

My question is: is there a way to prepare the JSON before transforming it to a feature class to avoid adding incorrect properties? Or, is there a way to ask Arc to behave itself with these missing properties?


Solution

  • I've found a solution using the JSON module's setdefault function.

    url_ = r"https://...."
    with urllib.request.urlopen(url_) as url:
        data = json.loads(url.read().decode('utf-8', 'replace'))
        for items in data['features']:
            if 'ID' not in items['properties']:
                del items
            else:
                items['properties'].setdefault("site_id", '000000000')
    with open(r'F:\JSON.json', 'w') as outfile:
        json.dump(data, outfile)