0

I have JavaScript in work order management software:

load("nashorn:mozilla_compat.js");
importPackage(java.util)
importPackage(Packages.psdi.server)
var ctx = new HashMap();
ctx.put("url","https://something.com/arcgis/rest/services/something/zones/MapServer/8/query?geometry=123456%2C1234567&geometryType=esriGeometryPoint&spatialRel=esriSpatialRelIntersects&outFields=*&returnGeometry=false&f=pjson");
service.invokeScript("LIB_HTTPCLIENT",ctx);
var jsonResp = ctx.get("response");
service.log(jsonResp);

The script sends a request (with coordinates) to a feature service:

https://something.com/arcgis/rest/services/something/zones/MapServer/8/query?geometry=123456%2C1234567&geometryType=esriGeometryPoint&spatialRel=esriSpatialRelIntersects&outFields=*&returnGeometry=false&f=pjson

The feature service (REST) returns information about the feature that the coordinates/point intersected with.

{
 "displayFieldName": "DAY",
 "fieldAliases": {
  "OBJECTID": "OBJECTID",
  "DAY": "DAY",
  "ZONE": "Area",
  "SHAPE.AREA": "SHAPE.AREA",
  "SHAPE.LEN": "SHAPE.LEN"
 },
 "fields": [
  {
   "name": "OBJECTID",
   "type": "esriFieldTypeOID",
   "alias": "OBJECTID"
  },
  {
   "name": "DAY",
   "type": "esriFieldTypeString",
   "alias": "DAY",
   "length": 10
  },
  {
   "name": "ZONE",
   "type": "esriFieldTypeString",
   "alias": "Area",
   "length": 6
  },
  {
   "name": "SHAPE.AREA",
   "type": "esriFieldTypeDouble",
   "alias": "SHAPE.AREA"
  },
  {
   "name": "SHAPE.LEN",
   "type": "esriFieldTypeDouble",
   "alias": "SHAPE.LEN"
  }
 ],
 "features": [
  {
   "attributes": {
    "OBJECTID": 327,
    "DAY": "Tuesday",
    "ZONE": "1",                     <-------zone is here
    "SHAPE.AREA": 5154109.08323004,
    "SHAPE.LEN": 10563.5744286387
   }
  }
 ]
}

I want to isolate the zone number from the JSON text so that I can insert it into a record/field.

How can I parse/isolate the zone number using JavaScript?

closed as off-topic by TomazicM, Vince, Fran Raga, Erik, neogeomat Aug 22 at 18:29

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions relating to general IT or with no clear GIS component, are off-topic here but can be researched/asked at Stack Overflow (software development), Super User (computing hardware and software), Database Administrators (relational databases) and other SE sites" – TomazicM, Vince, Fran Raga, Erik, neogeomat
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    features is an array with one element (note the square bracket). You're very close -try changing service.log(obj.features.attributes.zone) to service.log(obj.features[0].attributes.zone) – Steven Kay Aug 17 at 19:16
  • 1
    @StevenKay : A related question here: stackoverflow.com/q/57617494/10936066 – User1973 Aug 24 at 13:38
1

I can parse the JSON text using JSON.parse.

var obj = JSON.parse(jsonResp);
service.log(obj.features[0].attributes.ZONE)

Notes:

  1. The JSON features element is an array with a single element in it (note the square brackets).
    • Put an index reference after features: features[0]. Credit goes to @Steven Kay.
  2. The field name is case sensitive.
    • zone didn't work, but ZONE did.

That was harder than it needed to be!

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