How to parse JSON data in Script Include

Joshuu
Kilo Sage

Hi All,

I am working on a scripted rest API inbound integration with third party tool.

For that I have create a scripted rest API and a script include as below.

Scripted REST API:

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    // implement resource here
    var requestBody = request.body;
    var requestData = requestBody.data;
	var requestObj;
	
	if(requestData instanceof Array){
		
		for(requestObj in requestData){
			new InsertIncident().ProcessInsertIncident(requestData[requestObj], response);
		}
		
	}else{
		new InsertIncident().ProcessInsertIncident(requestData, response);
	}

})(request, response);

Script Include:

var InsertIncident = Class.create();
InsertIncident.prototype = {
        initialize: function() {},

        ProcessInsertIncident: function(requestData, response) {

            var validJSON = this._checkJSONValidity(requestData);
            var reqeustJSONParser = JSON.parse(validJSON);

            var responseBody = {};

            if (validJSON == true) {

                var inc = new GlideRecord('incident');
                inc.addQuery('correlation_id', reqeustJSONParser.id);
                inc.query();

                if (inc.next()) {
                    inc.caller_id = reqeustJSONParser.caller_id;
                    inc.short_description = reqeustJSONParser.short_description;
                    inc.description = reqeustJSONParser.description;
                    inc.work_notes = reqeustJSONParser.work_notes;
                    inc.category = reqeustJSONParser.category;

                    inc.update();

                    responseBody.Message = 'Incident is Updated';

                } else {

                    inc.initialize();
                    inc.caller_id = reqeustJSONParser.caller_id;
                    inc.short_description = reqeustJSONParser.short_description;
                    inc.description = reqeustJSONParser.description;
                    inc.comments = reqeustJSONParser.comments;
                    inc.correlation_id = reqeustJSONParser.id;

                    inc.insert();

                    responseBody.Message = 'Incident is Created';
                    responseBody.Number = inc.number;
                }

                response.setBody(responseBody);
			}
            },

            _checkJSONValidity: function(validJSON) {
                    try {
                        JSON.parse(validJSON);
                    } catch (e) {
                        return false;
                    }
                    return true;
                },


                type: 'InsertIncident'

        };

And I am not able to create/update incidents. In case if I change my script include as below, I am receiving the data. But I want to parse the JSON data before creating/updating. 

var InsertIncident = Class.create();
InsertIncident.prototype = {
        initialize: function() {},

        ProcessInsertIncident: function(requestData, response) {

            var responseBody = {};

                var inc = new GlideRecord('incident');
                inc.addQuery('correlation_id', reqeustJSONParser.id);
                inc.query();

                if (inc.next()) {
                    inc.caller_id = reqeustJSONParser.caller_id;
                    inc.short_description = reqeustJSONParser.short_description;
                    inc.description = reqeustJSONParser.description;
                    inc.work_notes = reqeustJSONParser.work_notes;
                    inc.category = reqeustJSONParser.category;

                    inc.update();

                    responseBody.Message = 'Incident is Updated';

                } else {

                    inc.initialize();
                    inc.caller_id = reqeustJSONParser.caller_id;
                    inc.short_description = reqeustJSONParser.short_description;
                    inc.description = reqeustJSONParser.description;
                    inc.comments = reqeustJSONParser.comments;
                    inc.correlation_id = reqeustJSONParser.id;

                    inc.insert();

                    responseBody.Message = 'Incident is Created';
                    responseBody.Number = inc.number;
                }

                response.setBody(responseBody);
			
            },
                type: 'InsertIncident'

        };

Please assist and correct me if I am wrong.

 

Thanks,

Priya.

1 ACCEPTED SOLUTION

Hello priya ,

You can add try catch for it in your function while you are stringifying it like below

_checkJSONValidity: function(validJSON)

{

try

{

return JSON.stringify(validJSON);

}

catch(e) { gs.info('parsing error'+e); }

}

Please mark my response as correct of it helped you 

View solution in original post

14 REPLIES 14

It is working Mohith, I am able to create and update the incidents and I do have a question, will it throw an error if the JSON is not valid ?

Hello priya ,

You can add try catch for it in your function while you are stringifying it like below

_checkJSONValidity: function(validJSON)

{

try

{

return JSON.stringify(validJSON);

}

catch(e) { gs.info('parsing error'+e); }

}

Please mark my response as correct of it helped you 

Hi Mohith,

I would like to update few fields based on incident state.

for example, the json will be different when the incident is 'New', and the json is different with different set of fields when it is 'On hold' and same as with 'Resolved'.

How can I add that state condition here?

could you please help me.

 

Thanks,

Priya.

hello @priyarao ,

can you please raise a new question for this and tag me i would definitely help you.

As this is another question which is inside the thread it might confuse people who are searching with same type of queries ?

also can you please mark this answer correct if the above answer solved the issue ?

Sure, thank you.