Can you parse JSON responses?

413
1
Jump to solution
04-24-2018 11:47 AM
MollyFoley
Regular Contributor

I have a geoprocessing service with a bunch of custom errors built in. If an error occurs, I would like to get the error message and return it to the user, but the results of a geoprocessing service are these huge JSON responses. Is there a way to easily parse these? 

For instance, this is the ESRI job failure response, all I want is the line in red below to return the user:

esriJobFailed(j62e4482338eb4149a0f8e9a6e574cd90):{"messages":[{"type":"esriJobMessageTypeInformative","description":"Submitted."},{"type":"esriJobMessageTypeInformative","description":"Executing..."},{"type":"esriJobMessageTypeInformative","description":"Executing (HabitatClassification): HabitatClassification C:\\arcgisserver\\directories\\arcgissystem\\arcgisuploads\\services\\MollyGPTesting\\HabitatClassification.GPServer\\ie6130b0b-b340-47e6-8b46-dcf2926abbe2\\VA_Habitat.gdb.zip AL"},{"type":"esriJobMessageTypeInformative","description":"Start Time: Tue Apr 24 14:44:29 2018"},{"type":"esriJobMessageTypeInformative","description":"Executing (HabitatClassification): HabitatClassification C:\\arcgisserver\\directories\\arcgissystem\\arcgisuploads\\services\\MollyGPTesting\\HabitatClassification.GPServer\\ie6130b0b-b340-47e6-8b46-dcf2926abbe2\\VA_Habitat.gdb.zip AL"},{"type":"esriJobMessageTypeInformative","description":"Start Time: Tue Apr 24 14:44:29 2018"},{"type":"esriJobMessageTypeInformative","description":"Running script HabitatClassification..."},{"type":"esriJobMessageTypeInformative","description":"Executing script \"HabitatClassification\"..."},{"type":"esriJobMessageTypeInformative","description":"Start Time 04/24/2018 14:44.29..."},{"type":"esriJobMessageTypeInformative","description":"c:\\arcgisserver\\directories\\arcgisjobs\\mollygptesting\\habitatclassification_gpserver\\j62e4482338eb4149a0f8e9a6e574cd90\\scratch\\scratch.gdb"},{"type":"esriJobMessageTypeInformative","description":"c:\\arcgisserver\\directories\\arcgisjobs\\mollygptesting\\habitatclassification_gpserver\\j62e4482338eb4149a0f8e9a6e574cd90\\scratch"},{"type":"esriJobMessageTypeInformative","description":"Successfully extracted zip file..."},{"type":"esriJobMessageTypeInformative","description":"c:\\arcgisserver\\directories\\arcgisjobs\\mollygptesting\\habitatclassification_gpserver\\j62e4482338eb4149a0f8e9a6e574cd90\\scratch\\VA_Habitat.gdb"},{"type":"esriJobMessageTypeError","description":"You have renamed a field from the original template. Please return it to its original state."},

0 Kudos
1 Solution

Accepted Solutions
MollyFoley
Regular Contributor

I think I figured out a way to do this, even if it's a bit clunky it'll do what I need it to.

First, you parse the JSON response in javascript using JSON.parse(). This will return an object with properties of messages, jobId, and jobStatus. The messages object may be an array, so you can loop through the array of messages and look for type "esriJobMessageTypeError". These are your error messages. If a message matches that type you can access the "description" which is the actual error message.

My code looks a little something like this now:

function gpJobComplete(result) {
              console.log(result.jobStatus + '(' + result.jobId + ')');
              var jsonResults = dojo.toJson(result);
              var jsonParsed = JSON.parse(jsonResults);
              for (var i = 0; i < jsonParsed.messages.length; i++) {
                  var message = jsonParsed.messages[i];
                  if (message.type == "esriJobMessageTypeError") {
                      console.log("My error message: " + message.description);
                  };
              }

View solution in original post

1 Reply
MollyFoley
Regular Contributor

I think I figured out a way to do this, even if it's a bit clunky it'll do what I need it to.

First, you parse the JSON response in javascript using JSON.parse(). This will return an object with properties of messages, jobId, and jobStatus. The messages object may be an array, so you can loop through the array of messages and look for type "esriJobMessageTypeError". These are your error messages. If a message matches that type you can access the "description" which is the actual error message.

My code looks a little something like this now:

function gpJobComplete(result) {
              console.log(result.jobStatus + '(' + result.jobId + ')');
              var jsonResults = dojo.toJson(result);
              var jsonParsed = JSON.parse(jsonResults);
              for (var i = 0; i < jsonParsed.messages.length; i++) {
                  var message = jsonParsed.messages[i];
                  if (message.type == "esriJobMessageTypeError") {
                      console.log("My error message: " + message.description);
                  };
              }