Form JSON payload for a incident record

varunkumar1
Tera Contributor

Hi Team,

 

I want to form JSON payload for an incident record with all field names and their values . Kindly help me with any ideas.

 

Thanks

4 REPLIES 4

Harsh Vardhan
Giga Patron

 

Kindly have a look on below blog. it has mentioned the script.

 

http://www.john-james-andersen.com/blog/service-now/converting-gliderecord-to-json-in-servicenow.htm...

 

If my answer helped you, mark it as correct and helpful.

Harsh Vardhan
Giga Patron

 

 

Another way

 

Sample Code:

 


var gr = new GlideRecord("incident");
gr.addQuery('sys_id','9a38b7092f030410f68d5ff62799b6d3');
gr.query();
gr.next();


var obj = {};
for (var i in gr) {
if (!{}.hasOwnProperty.call(obj, i)){
obj[i] = gr[i].toString();
}
}
gs.print(JSON.stringify(obj));

 

If my answer helped you, mark it as correct and helpful.

 

Baala T
Mega Guru

Hi Varun,

Try this code,

var current = new GlideRecord('incident');
current.get('57af7aec73d423002728660c4cf6a71c');

var fields = current.getFields();

var field;
var name = '';
var value = '';

var object_data = {};

for (var i = 0; i < fields.size(); i++) {
    name = '';
    value = '';

    field = fields.get(i);
    name = field.getName();
    value = current.getValue(name);

    object_data[name] = value;
}

var json = new JSON();
var sObjectData = json.encode(object_data);
gs.print(sObjectData);

 

Regards,
Bala T

im_atrey
Tera Contributor

Hi Varun,

There are always many ways to perform the logic, the instance has script include, JSON which you need to instantiate as an object then you can invoke the desired function either convert it to string or parse the specific value.

 

Below is the Script include:

gs.include("PrototypeServer");

var JSON = Class.create();
JSON.prototype = {
    indentFlag: false,
    indentLevel: 0,
    INDENT_SPACES: "  ",

    initialize: function() {

    },

    prettify: function() {
        this.indentFlag = true;
        this.indentLevel = 0;
    },

    startIndent: function(a) {
        if (this.indentLine(a, true))
            this.indentLevel++;
    },

    endIndent: function(a) {
        if (!this.indentFlag)
            return;

        this.indentLevel--;
        this.indentLine(a);
    },

    indentLine: function(a, skipNewLine) {
        if (!this.indentFlag)
            return false;

        if (!skipNewLine)
            a.push("\n");

        for (var i = 0; i < this.indentLevel; i++)
            a.push(this.INDENT_SPACES);

        return true;
    },

    pad: function(n) {
        return n < 10 ? "0" + n : n;
    },

    encodeString: function(s) {
        return '"' + GlideStringUtil.escapeNonPrintable(s) + '"';
    },

    encodeArray: function(o) {
        var a = [ "[" ];
        this.startIndent(a);
        var b;
        var i;
        var l = o.length;
        var v;
        for ( var i = 0; i < l; i += 1) {
            v = o[i];
            switch (typeof v) {
            case "undefined":
            case "function":
            case "unknown":
                break;
            default:
                if (b) {
                    a.push(',');
                }
                this.indentLine(a);
                a.push(v === null ? "null" : this.encode(v));
                b = true;
            }
        }
        this.endIndent(a);
        a.push("]");
        return a.join("");
    },

    getKeys: function(o) {
        var a = [];
        for ( var i in o) {
            a.push(i);
        }
        a.sort();
        return a;
    },

    encodeObject: function(o) {
        var a = [ "{" ];
        this.startIndent(a);
        var b;
        var i;
        var v;
        var keys = this.getKeys(o);
        for ( var i = 0; i < keys.length; i++) {
            if (o.hasOwnProperty(keys[i])) {
                v = o[keys[i]];

                switch (typeof v) {
                case "undefined":
                case "function":
                case "unknown":
                    break;
                default:
                    if (b) {
                        a.push(',');
                    }
                    this.indentLine(a);
                    a.push(this.encode(keys[i]), ":", v === null ? "null" : this.encode(v));
                    b = true;
                }
            }
        }

        this.endIndent(a);
        a.push("}");
        return a.join("");
    },

    encodeDate: function(o) {
        return '"' + o.getFullYear() + "-" + this.pad(o.getMonth() + 1) + "-" + this.pad(o.getDate()) + "T"
                + this.pad(o.getHours()) + ":" + this.pad(o.getMinutes()) + ":" + this.pad(o.getSeconds()) + '"';
    },

    encode: function(o) {
        if (typeof o == "undefined" || o === null) {
            return "null";
        } else if (o instanceof Array) {
            return this.encodeArray(o);
        } else if (o instanceof Date) {
            return this.encodeDate(o);
        } else if (typeof o == "string" || o instanceof String) {
            return this.encodeString(o);
        } else if (typeof o == "number" || o instanceof Number) {
            return isFinite(o) ? String(o) : "null";
        } else if (typeof o == "boolean" || o instanceof Boolean) {
            return String(o);
        } else {
            return this.encodeObject(o);
        }
    },

    decode: function(source) {
        return new SNC.JSONParse().decode(source);
    },

    type: "JSON"
};

/**
 * Proxies calls to the ES5 JSON object, named NativeJSON in the global scope.
 */
JSON.parse = function(text, reviver) {
	return NativeJSON.parse.apply(NativeJSON, arguments);
};

/**
 * Proxies calls to the ES5 JSON object, named NativeJSON in the global scope.
 */
JSON.stringify = function(value, replacer, space) {
	return NativeJSON.stringify.apply(NativeJSON, arguments);
};



 

In your server-side script/Client-side you only need to use their functions:

  • JSON.stringify()
  • JSON.Parse()

Although there are many other functions, as you only need to convert the whole form in JSON, then you need to use JSON.stringify(<Object name>);

 

Kindly mark it, if helpful 🙂

Regards

Aashish