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

Try placing this statement inside if(validJSON)

Hi Muhammad,

Updated the code as below, but still facing the issue.

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

    ProcessInsertIncident: function(requestData, response) {

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

        var responseBody = {};

        if(validJSON) {

            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'
};

I was saying that try something like below

if(validJSON){
    var reqeustJSONParser = JSON.parse(requestData);
}

// Also try using

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

Mohith Devatte
Tera Sage
Tera Sage

hello @priyarao ,

try using this in your parsing function 

  _checkJSONValidity: function(validJSON) {
                   
                      return JSON.stringify(validJSON);
                  
                },

Rest everything keep as is and try which is  and continue what you were doing in the rest of the code and try

 ProcessInsertIncident: function(requestData, response) {

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

please mark my answer correct if it helps you

 

 

Thank you Mohith.

It is working fine now. Incident is getting created and I am able to update it too.