

- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
Introduction:
This article is enhancement of my old article i.e. JSON Parsing. I got few comments on that article where people wanted to achieve few things. I will be dividing them into use case and then explain them in detail. Mostly this will be focused on core JSON concepts as we use JSON frequently to get data from multiple place and use this JSON object to setvalue for the fields, take decision based on json length or object,etc.
Use Case 1 : How to create simple JSON object and Complex nested JSON Object.
1.1 Create a simple JSON Object: Assume you want to get the details of current logged in user in JSON object like email id, manager and title,etc
Code:
var obj = {};
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', gs.getUserID());
gr.query();
if (gr.next()) {
obj['email']= gr.email.toString();
obj['title'] = gr.title.toString();
obj['manager'] = gr.manager.toString();
}
var UserInfo = JSON.stringify(obj);
gs.info(UserInfo);
Result:
{
"email":"admin@example.com",
"title":"System Administrator",
"manager":"5137153cc611227c000bbd1bd8cd2005"
}
1.2 Create a nested JSON Object : This is very useful when you do integration with an external system which has a different JSON structure and are not robust to accept JSON object in different format. I have also seen some case’s where people from other tools want displayvalue and value of the field from ServiceNow for example take manager field. In above example we are just sending the sys_id of the user and not user id and name. So we will see how to do that.
Code:
var obj = {};
var manager = {};
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', gs.getUserID());
gr.query();
if (gr.next()) {
obj['email']= gr.email.toString();
obj['title'] = gr.title.toString();
manager['DisplayName'] = gr.manager.name.toString();
manager['value'] = gr.manager.toString();
manager['user_name'] = gr.manager.user_name.toString();
obj['manager'] = manager;
}
var UserInfo = JSON.stringify(obj);
gs.info(UserInfo);
Result:
{
"email":"admin@example.com",
"title":"System Administrator",
"manager":{
"DisplayName":"Fred Luddy",
"value":"5137153cc611227c000bbd1bd8cd2005",
"user_name":"fred.luddy"
}
}
1.3 Create a JSON object with arrays and nested objects: This is interesting for most of us, Lets assume that we have a integration with third party tool which is requesting a CI data and CI relationships like Upstream and downstream. He wants this in only one request and he don’t want to make several or separate calls to ServiceNow to fetch this data along with the class details of the CI related to each other. So here we will create a main JSON object to with nested arrays and objects.
Example Dependency Map for CI:
Code:
var Up = [];
var down = [];
var mainJson = {};
var ci = '27e3a47cc0a8000b001d28ab291fa65b';
var getUp = new GlideRecord('cmdb_rel_ci');
getUp.addQuery('child',ci.toString());
getUp.query();
while(getUp.next()){
var obj = {};
obj['Class'] = getUp.parent.sys_class_name.toString();
obj['ci_name'] = getUp.parent.name.toString();
Up.push(obj);
}
var getDown = new GlideRecord('cmdb_rel_ci');
getDown.addQuery('parent',ci.toString());
getDown.query();
while(getDown.next()){
var obj = {};
obj['Class'] = getDown.child.sys_class_name.toString();
obj['ci_name'] = getDown.child.name.toString();
down.push(obj);
}
var gr = new GlideRecord('cmdb_ci');
if(gr.get(ci)){
mainJson['CI Name'] = gr.name.toString();
mainJson['Class Name'] = gr.sys_class_name.toString();
mainJson['sserial_number'] = gr.serial_number.toString();
}
mainJson['UpstreamRel'] = Up;
mainJson['DownstreamRel'] = down;
var ciInfo = JSON.stringify(mainJson);
gs.info(ciInfo);
Result:
{
"CI Name": "OWA-SD-01",
"Class Name": "cmdb_ci_win_server",
"sserial_number": "Testing1234",
"UpstreamRel": [
{
"Class": "cmdb_ci_service",
"ci_name": "Outlook Web Access (OWA)"
},
{
"Class": "cmdb_ci_service",
"ci_name": "Windows Mobile"
}
],
"DownstreamRel": [
{
"Class": "cmdb_ci_server",
"ci_name": "VMWARE-SD-04"
},
{
"Class": "cmdb_ci_server",
"ci_name": "VMWARE-SD-07"
}
]
}
Use Case 2: How to fetch Keys from JSON Object and Size of JSON Object.
In case one we saw how to create JSON object, nested JSON with arrays, now in this case we will see how to check the size of this JSON object and how to fetch the keys for this JSON object. We will consider the JSON generated in 1.3 scenario of above case.
1.1 To fetch Size and keys of mainJson object:
Code:
var myObject = {"CI Name":"OWA-SD-01","Class Name":"cmdb_ci_win_server","serial_number":"Testing1234","UpstreamRel":[{"Class":"cmdb_ci_service","ci_name":"Outlook Web Access (OWA)"},{"Class":"cmdb_ci_service","ci_name":"Windows Mobile"}],"DownstreamRel":[{"Class":"cmdb_ci_server","ci_name":"VMWARE-SD-04"},{"Class":"cmdb_ci_server","ci_name":"VMWARE-SD-07"}]}
var count = Object.keys(myObject);
gs.log('Keys :'+count);
var Size = Object.keys(myObject).length;
gs.log('Size of Object :'+Size);
Result:
Keys :CI Name,Class Name,serial_number,UpstreamRel,DownstreamRel
Size of Object :5
1.2 To Fetch the Keys and size of upstream and downstream array in mainJson:
Code:
var myObject = {"CI Name":"OWA-SD-01","Class Name":"cmdb_ci_win_server","serial_number":"Testing1234","UpstreamRel":[{"Class":"cmdb_ci_service","ci_name":"Outlook Web Access (OWA)"},{"Class":"cmdb_ci_service","ci_name":"Windows Mobile"}],"DownstreamRel":[{"Class":"cmdb_ci_server","ci_name":"VMWARE-SD-04"},{"Class":"cmdb_ci_server","ci_name":"VMWARE-SD-07"}]}
var count = Object.keys(myObject.UpstreamRel[0]);
gs.log('Keys :'+count);
var Size = Object.keys(myObject.UpstreamRel).length;
gs.log('Size of Object :'+Size);
var count = Object.keys(myObject.DownstreamRel[0]);
gs.log('Keys :'+count);
var Size = Object.keys(myObject.DownstreamRel).length;
gs.log('Size of Object :'+Size);
Result:
Keys :Class,ci_name
Size of Object :2
Keys :Class,ci_name
Size of Object :2
Thanks for reading. Please keep providing comments and use case's so that i can create more blogs.
Please don’t forget to mark helpful and bookmark this article.
Thanks and Regards,
Ashutosh Munot
- 3,900 Views
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.