how to parse json string and display it in ui page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2017 04:05 AM
I need to display the first five created asset names and asset category according to their creation date in a Ui page in table format . i have written script include
--script include ---
var getAssetClass = Class.create();
getAssetClass.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getAsset: function() {
var gr = new GlideRecord('alm_asset');
gr.orderBy('sys_created_on');
gr.setLimit(5);
gr.query();
var assets=[];
while(gr.next()){
asset={};
var asset_name = gr.getDisplayValue('display_name');
asset.name=asset_name;
asset.category=gr.getDisplayValue('model_category');
assets.push(asset);
//gs.print(asset.name+"!!!!!!!!!!"+asset.category);
}
//gs.print(assets);
var assetsString = JSON.stringify(assets);
return assetsString;
},
//getDisplay: function() {
// return 10;
//},
type: "getAssetClass"
});
-- client script for testing---------
var ga = new GlideAjax('getAssetClass');
ga.addParam('sysparm_name','getAsset');
//ga.addParam('sysparm_name1','getDisplay');
//ga.addParam('sysparm_user_name',"Bob");
ga.getXML(HelloFun);
function HelloFun(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
//console.log(answer);
alert(answer);
}
i am not able to parse the JSON string and display it in ui page in table format using HTML
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2017 04:23 AM
Hi Suprakash,
you need to use parse() method
use it as belows:
var jsonData = JSON.parse(assets);
var asset_name = parsedData[name];
var category = parsedData[category];
this is client side.
If you want to use in server side then it is as belows
var jsonData = new JSONParser();
var parsedData = jsonData.parse(assets);
var asset_name = parsedData.name;
var category = parsedData.category;
Mark my reply as Correct and also hit Like and Helpful if you find my response worthy.
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-09-2017 10:39 PM
Thanks Ankur,
i am writing the client client script in the ui page, and the alert is showing . But i am not able to use it withing the HTML part. How to connect the client script with the HTML .. does we need to use the processing script?????
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2017 11:18 PM
Hi Suprakash,
Once you get the value from the json string you can use document.getElementById('id1').innerHTML to set the html value.
Mark my reply as Correct and also hit Like and Helpful if you find my response worthy.
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-09-2017 11:32 PM
Hi Suprakash,
In order to connect the client script to HTML you need to create variable of hidden type in HTML and then use that variable in Client script.
Do you need an example for this?
Regards,
Shamma