0

I need to run a daily script that checks if data hosted on AGOL matches source data in an enterprise geodatabase. I'd like to convert the enterprise geodatabase table and a JSON file containing the feature layer data on AGOL to Pandas dataframes to facilitate the data comparisons but I'm having an issue grabbing the JSON from AGOL:

import arcgis
from arcgis.gis import GIS
from arcgis.features import FeatureLayer
import json

gis = GIS(portal, user, password)

agolLyr = gis.content.search("my_feature_layer", item_type="Feature Layer")
print (agolLyr)

my_feature_layer1
my_feature_layer2

item = gis.content.get("agolLyr[0]")
#item_data = item.get_data()
print(json.dumps(item, indent=4, sort_keys=True))

AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_12420\1851565882.py in <cell line: 2>()
      1 item = gis.content.get("agolLyr[0]")
----> 2 item_data = item.get_data()
      3 print(json.dumps(item, indent=4, sort_keys=True))

AttributeError: 'NoneType' object has no attribute 'get_data'

I'm pretty sure this is the wrong line to access the attribute data in JSON form but not sure what is. I feel like I have been able to access the data attributes in a JSON array through AGOL in the past but I can't seem to get there now.

9
  • For a hosted feature layer, you can access it without the arcgis API, by using an HTTP request within your Python. To get the request URL, open the layer's "Overview" page in AGOL, then click on the "Feature Service" link under the "Details" section (towards the bottom-right). Click the specific layer you need. Scroll to the bottom and click "Query". Enter the query details in the form, choosing "JSON" (ESRI JSON) or "GeoJSON" as the 'Format'. Click 'Query (GET'). The next page will have the current JSON, but also will be the URL that you can use anytime in a script request... May 3, 2023 at 23:43
  • ...if using that method, you may also have to play around with authentication tokens. May 3, 2023 at 23:46
  • @SonofaBeach Thank you. I'm able to create the JSON manually though the REST services interface but it creates a url with an id. When this is used in the requests.get(lyr),json() it gives JSONDecodeError: Expecting value: line 1 column 1 (char 0). When I plug the url in manually in a new tab I get back tot the feature server page instead of the json page.
    – geoJshaun
    May 5, 2023 at 21:59
  • @SonofaBeach myagol.maps.arcgis.com/home/… the query I'm using is WHERE OBJECTID>0, OUT FILEDS *, RETURN GEOMETRIES FALSE, QUERY (GET)
    – geoJshaun
    May 9, 2023 at 19:46
  • sorry, I should have been more specific... What is the URL of the page that you get after hitting the "Query (GET)" button? This URL should include all of the parameters that you used in the query form. May 10, 2023 at 0:22

1 Answer 1

1

'The problem is that the REST service URL is not formatting correctly to access the data as it navigates to REST service overview and not an array of data. Tried to overcome this by entering request parameters on the feature service page (FeatureServer/0) using the QUERY(GET) option for the JSON format (WHERE: OBJECTID > 0, RETURN GEOMETRY: FALSE, OUTPUT FIELDS: *).

This returned the expected data array but the URL did not change with the desired parameters in the URL QUERY string. This was a requirement as I am building a pandas dataframe with the data and need to call on the same array in Python on a daily basis to track changes in the feature layer's data.

After some trial and error and reading Eri's page on building QUERY strings under REST Services: https://developers.arcgis.com/rest/services-reference/enterprise/query-feature-service-layer-.htm

I was able to construct the following string format that returned the desired json array:

https://myorganization.arcgis.com/myOrganizationID/ArcGIS/rest/services/My_Feature_Service_Name/FeatureServer/0/query?where=1=1&objectIds=&f=pjson&outFields=*&returnGeometry=false

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.