I am trying to automate a process by which I can query an ArcGIS map server, take the resulting text and save it as a .json file.
The map server can be queried through an API.
api = "https://csg.esri-southafrica.com/server/rest/services/CSGSearch/MapServer/2/query?where=1%3D1&text=&objectIds=&time=&geometry=2053965%2C-4019103%2C2054056%2C-4019169+&geometryType=esriGeometryEnvelope&inSR=3857&spatialRel=esriSpatialRelIntersects&relationParam=&outFields=*&returnGeometry=true&returnTrueCurves=false&maxAllowableOffset=&geometryPrecision=&outSR=3857&returnIdsOnly=false&returnCountOnly=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&returnZ=false&returnM=false&gdbVersion=&returnDistinctValues=false&resultOffset=&resultRecordCount=&queryByDistance=&returnExtentsOnly=false&datumTransformation=¶meterValues=&rangeValues=&f=pjson"
This URL is not that important but when I run this in a browser it gives this response:
{
"displayFieldName": "SP_NAME",
"fieldAliases": {
"OBJECTID": "OBJECTID",
"GID": "Geometry Identifier",
"PRCL_KEY": "26 Digit Code",
"PRCL_TYPE": "Parcel Type",
"LSTATUS": "Legal Status",
"WSTATUS": "Work Status",
"GEOM_AREA": "Geometry Area",
"COMMENTS": "Comments",
"TAG_X": "Longitude",
etc.etc.etc
If I copy this text to notepad and save as "any_file.json". I can then load this in QGIS and save it as a shapefile.
I have been using the following code to try and achieve this
import requests
mainapi = "https://csg.esri-southafrica.com/server/rest/services/CSGSearch/MapServer/2/query?where=1%3D1&text=&objectIds=&time=&geometry=2053965%2C-4019103%2C2054056%2C-4019169+&geometryType=esriGeometryEnvelope&inSR=3857&spatialRel=esriSpatialRelIntersects&relationParam=&outFields=*&returnGeometry=true&returnTrueCurves=false&maxAllowableOffset=&geometryPrecision=&outSR=3857&returnIdsOnly=false&returnCountOnly=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&returnZ=false&returnM=false&gdbVersion=&returnDistinctValues=false&resultOffset=&resultRecordCount=&queryByDistance=&returnExtentsOnly=false&datumTransformation=¶meterValues=&rangeValues=&f=pjson"
r = str(requests.get(mainapi).json())
#Write response to json text file
with open("csg_erven.json", "w") as f:
f.write(r)
The results in the .json file are not formatted
they appear as such:
{'displayFieldName': 'SP_NAME', 'fieldAliases': {'OBJECTID': 'OBJECTID', 'GID': 'Geometry Identifier', 'PRCL_KEY':
I am new to coding in general, but I am assuming the formatting is crucial here. How can I copy the text with formatting? Is it an encoding issue?
When I manually copy the formatted text from a browser it works fine, but the single line text does not work.
Any help would be greatly appreciated.
Looks to me like there's a bit of a mixup in how you're saving the file. Try using the json library rather than converting to a string and saving as text which could lead to problems
import json
with open('csg_erven.json', 'w') as f:
json.dump(requests.get(mainapi).json(), f)