JSON object in for loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2017 07:14 AM
Hi All,
I have a json object inside which der is an array called as items which is used to store the RITM of a request.
But suppose i have 4 RITM for a request how can i dynamically create 4 elements inside the array with these field values.
{
"Items": [{
"MaterialNumber": "3000016264859",
"CustMaterialNumber": "RITM00098",
"LineNumber": "1",
"HighLevelItem": "",
"Quantity": "2",
"SmartTrackerItem": [{
"Key": "",
"Value": ""
}]
}]
}
Thanks in advance.
Abinash Dash
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2017 07:19 AM
You can add more to the array by comma-separating the various objects. Something like:
{
"Items": [
{
"Material number":"xyz",
//object 1 details
},
{
//object 2 details
},
{
//object 3 details
}]
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2017 07:50 AM
Hi adam,
Thanks for ur reply but creation of ritm is dynamic..so how can i handle
the for loop for this json array so that it shoukd automatically provide me
the resulted format
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2017 07:57 AM
Hi Abinash,
Maybe this will help:
obj['items'].push({key:value});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2017 07:59 AM
OK, let me take a stab at this. In the script below I'm assuming you're looping through the RITMs associated to a particular Request. Hopefully this helps.
var jsonObj = {};
jsonObj.Items = [];
var ritmRec = new GlideRecord('sc_req_item');
ritmRec.addQuery('request', requestID); //requestID will be the sys_id of the Request in question
ritmRec.query();
while(ritmRec.next()) {
itemObj = {};
itemObj.my_details = ritmRec.my_details; //add in all of the mapping that you want here
jsonObj.Items.push(itemObj);
}
At the end of this the jsonObj object should be what you're looking for. You can use JSON.stringify(jsonObj) to convert it into a string if need be.
