- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-18-2017 11:17 PM
How to parse array of objects which are in json.?
I Have a response
[{"UserID":"10001","Name":"Ram"},
{"UserID":"10002","Name":"Sultana"},
{"UserID":"10003","Name":"Lakshmi"}]
As of now i have three records which i mentioned above, now how can i parse it? and add it into my 'User'(sys_user) table? which am running in background script.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-19-2017 11:17 PM
Hi Swathi,
Copy and paste the response and store it in a string variable and execute this script in background and check length and whether values are fetched properly or not
var string = ""; // copy and paste your response in this variable
var parser = new JSONParser();
var parsedData = parser.parse(string);
var length = parsedData.length;
gs.print(length);
var userIDArray = [];
var nameArray = [];
for(var i=0;i<length;i++){
gs.print(parsedData[i].UserID);
gs.print(parsedData[i].Name);
userIDArray.push(parsedData[i].UserID.toString());
nameArray.push(parsedData[i].Name.toString());
}
gs.print('user id array is:'+userIDArray);
gs.print('name array is:'+nameArray);
Execute this script and share a snapshot of what you get in background
Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-19-2017 10:48 PM
Hi Ankur,
I added log message,replaced two lines as you said.. still undefined undefined... Am not able to fetch the field level values from REST response .
Here is the code i am using,
try {
var r = new sn_ws.RESTMessageV2('Test', 'Default GET');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var parser = new JSONParser();
var parsedD= parser.parse(responseBody);
gs.print(parsedD);
gs.log(parsedD);
var parsedData=parsedD;
var length = parsedData.length;
gs.print(length);
gs.log(length);
var userIDArray = [];
var nameArray = [];
for(var i=0;i<10;i++){
gs.print(parsedData[i].UserID);
gs.log(parsedData[i].UserID);
userIDArray.push(parsedData[i].UserID.toString());
nameArray.push(parsedData[i].Name.toString());
gs.print(parsedData[i].Name);
gs.log(parsedData[i].Name);
}
gs.print('user id array is:'+userIDArray);
gs.print('name array is:'+nameArray);
}
catch(ex) {
var message = ex.getMessage();
}
---------
Am getting response properly as array of objects but not able to split that array .... Kindly go through the above code and please tell me what modifications are needed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-19-2017 11:17 PM
Hi Swathi,
Copy and paste the response and store it in a string variable and execute this script in background and check length and whether values are fetched properly or not
var string = ""; // copy and paste your response in this variable
var parser = new JSONParser();
var parsedData = parser.parse(string);
var length = parsedData.length;
gs.print(length);
var userIDArray = [];
var nameArray = [];
for(var i=0;i<length;i++){
gs.print(parsedData[i].UserID);
gs.print(parsedData[i].Name);
userIDArray.push(parsedData[i].UserID.toString());
nameArray.push(parsedData[i].Name.toString());
}
gs.print('user id array is:'+userIDArray);
gs.print('name array is:'+nameArray);
Execute this script and share a snapshot of what you get in background
Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-02-2017 01:25 AM
Hi Swathi,
Were you able to achieve the requirement. If yes then please mark the answer as correct and also hit like. This helps in closing the thread for the question.
If any issue in achieving the requirement then please mention the same so that it could be achieved.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-01-2017 10:49 AM
Hi Swathi,
Were you able to achieve your requirement? If yes then please mark the answer as correct, helpful and hit like. This helps in closing the thread for the question. Thanks in advance.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-23-2017 12:28 AM
Hi,
I believe this is the Right place to put up my question. I am doing a simple query and trying to put the contents in an object. But when I try to log the same, its giving
as [Object][Object]. Any idea what I need to do for this?
var lis = [];
var incDetails = {};
//........................................ Block starting for aray..............//
var sysid = [];
sysid = this.getParameter('sysparm_valueap').split(',');
for(var j=0; j<sysid.length; j++){
var incd = new GlideRecord('incident');
incd.addQuery('sys_id',sysid[j]);
incd.query();
while(incd.next()){
incDetails = {};
incDetails.number = incd.number;
incDetails.email = incd.assigned_to.email;
incDetails.sys_id = incd.sys_id;
lis.push(incDetails);
}
}
gs.log("Details are:"+lis);