I had a customer ask me today how to easily convert a Glide Record object to JSON format. They were using the JSON Web Services Plugin and wanted to send data from ServiceNow to a JSON Web Service.
I reviewed the OOB JSON class that does object encoding and found that it didn’t have a dedicated method for encoded GlideRecord objects.
I wrote a quick Script Include that inherits from the JSON class and adds a function called “encodeGr”.
Here is how you can do it:
SCRIPT INCLUDE NAME: JSONG
SCRIPT:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | var JSONG = Class.create(); JSONG.prototype = Object.extendsObject(JSON, { encodeGr : function(o) { var a = ["{"], b, i, v; for (i in o) { if(o.hasOwnProperty(i)) { v = o[i].toString(); switch (typeof v) { case "undefined": case "function": case "unknown": break; default: if (b) { a.push(','); } a.push(this.encode(i), ":", v === null ? "null" : this.encode(v)); b = true; } } } a.push("}"); return a.join(""); } }); |
Then to test it, you can go to your “Background Scripts” module and run the following script:
1 2 3 4 5 6 7 8 | var gr = new GlideRecord("incident"); gr.query(); gr.next(); gs.print(gr.short_description); var g = new JSONG(); var t = g.encodeGr(gr); gs.print(t); |
This should return your GlideRecord in the following format:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | { "assigned_to": "", "category": "inquiry", "escalation": "3", "state": "1", "location": "", "reassignment_count": "0", "time_worked": "", "order": "", "due_date": "", "number": "INC0010001", "upon_approval": "proceed", "sla_due": "2010-09-15 17:31:07", "follow_up": "", "notify": "1", "business_stc": "", "caused_by": "", "rejection_goto": "", "assignment_group": "d625dccec0a8016700a222a0f7900d06", "incident_state": "1", "opened_at": "2010-08-25 17:31:07", "wf_activity": "", "calendar_duration": "", "group_list": "", "caller_id": "", "comments": "", "priority": "4", "sys_id": "aa4d9e0bc0a8016b00c96a8605090a6d", "sys_updated_by": "system", "variables": "", "delivery_task": "", "sys_updated_on": "2011-04-14 20:50:43", "parent": "", "active": "true", "opened_by": "glide.maint", "expected_start": "", "sys_meta": "System meta data", "watch_list": "", "company": "", "upon_reject": "cancel", "work_notes": "", "sys_created_by": "glide.maint", "cmdb_ci": "", "approval_set": "", "user_input": "", "sys_created_on": "2010-08-25 17:32:00", "contact_type": "phone", "rfc": "", "approval_history": "", "activity_due": "", "severity": "3", "subcategory": "", "work_end": "", "closed_at": "", "close_notes": "", "variable_pool": "", "business_duration": "", "knowledge": "false", "approval": "not requested", "sys_mod_count": "3", "problem_id": "", "calendar_stc": "", "work_start": "", "sys_domain": "global", "correlation_id": "", "sys_class_name": "incident", "short_description": "No one told me about the crash", "impact": "3", "description": "", "correlation_display": "", "urgency": "3", "made_sla": "true", "delivery_plan": "", "closed_by": "" } |
Kenny Caldwell suggests that you name your new child class something a little different so that “JSON” is at the beginning of the class name so that it shows up with your other JSON script includes.
Some suggestions are:
* JSONG
* JSONGlide
Use your imagination!
we have JSON go for JDAD….
The real dadyy