FujiForty - What Happened to JSONParser?

Let's get back to some more API changes in the Fuji release. Anyone who has done any integration work in ServiceNow has most likely used JSONParser at least once. Unfortunately, this is yet another API that is not available in non-global application scopes.

No need to worry, there is an alternative way to parse JSON strings using the JSON class script include. Here's an example of decoding a JSON string into an object, working with the object, making changes and finally converting the object back to a string.

//source JSON string
var flightDataString = '{
pilot:{name:"John Roberts",pic_hours:"150",home_airport:"KHFD"}, 
aircraft:{id:"N113PF", model:"C172R", burn_rate:"9"}, 
route:"KHFD  KHTO  KGON  0B8  KHFD"}';

//parse JSON string to object
var flightObject = 
    new global.JSON().decode(flightDataString);

//access object properties
gs.debug("{0} is piloting a {1} on a route of {2}",
         flightObject.pilot.name, 
         flightObject.aircraft.model,
         flightObject.route);

//modify the object by adding a new property
flightObject.status = "Completed";

//convert object back to a string
var updatedFlightDataString = 
    new global.JSON().encode(flightObject);

//output the string
gs.debug(updatedFlightDataString);
*** Script: [DEBUG] John Roberts is piloting a C172R on a route of KHFD KHTO KGON 0B8 KHFD *** Script: [DEBUG] {"aircraft":{"burn_rate":"9","id":"N113PF","model":"C172R"},"pilot":{"home_airport":"KHFD","name":"John Roberts","pic_hours":"150"},"route":"KHFD KHTO KGON 0B8 KHFD","status":"Completed"}