- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2023 10:22 AM - edited 09-28-2023 05:04 AM
Hi,
How to get the value of repository id and Name from the source data field by using array?
source data: {"assetexposurescore":"497","repository":"{\"id\" : \"36\", \"name\" : \"(01) - Health Services - ActiveScan\", \"description\" : \"Health Services - ActiveScan\", \"dataFormat\" : \"IPv4\"}","score":"40","acrscore":"4.0",
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2023 08:39 PM
As it contains escape characters so we need to first remove the escape characters.
Refer the below Steps :
// 1.Store result in variable , Just for simplicity I have taken only some data from your JSON object
var result = {"assetexposurescore":"497","repository":"{\"id\" : \"36\", \"name\" : \"(01) - Health Services - ActiveScan\", \"description\" : \"Health Services - ActiveScan\", \"dataFormat\" : \"IPv4\"}"}
// 2. Stringyfy the result so that we can remove escape characters
var stResult = JSON.stringify(result);
// 3. replace the escape characters
var newResult = stResult.replace( /\'\'/g,'');
// 4. convert it into object again
var parser = JSON.parse(newResult);
// 5. get the repository object form newly created object
var repository = parser.repository;
//gs.info('repostory=' + repository);
// 6. Create new JSON objectas repository contains string
var newObj = {
repoData : {},
}
// 7. add parsed repository into new JSON
newObj.repoData = JSON.parse(repository);
// 8. get the values by dot walking
gs.info("Id = " + newObj.repoData.id);
gs.info("name = " + newObj.repoData.name);
Output :
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2023 05:02 AM
Hi @Vishal Birajdar ,
Issue has been resolved, I am sharing my updated fix script.
And Thank you so much for all your help, which really helps me a lot.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2023 01:30 AM
Hello @jugantanayak ,
Happy to help...!!
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2023 05:41 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2023 05:46 AM
Yes, you can use it in fix script.
The screenshot I have sent is from background script only.
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2023 07:11 PM
Hi @Vishal Birajdar ,
I have created below mentioned fix script, can you please check and suggest in case any modification required on it.
var res = new GlideRecord('sn_sec_cmn_src_ci');
res.addQuery('source_data);
res.setLimit();
res.query();
while(res.next()){
var result =(res.source_data);
var stResult = JSON.stringify(result);
var newResult = stResult.replace( /\'\'/g,'');
var parser = JSON.parse(newResult);
var repository = parser.repository;
var newObj = {
repoData : {},
}
newObj.repoData = JSON.parse(repository);
var id = newObj.repoData.id;
var name = newObj.repoData.name;
res.setValue('u_repository_id',id);
res.setValue('u_repository_name',name);
res.setWorkflow(false);
res.autoSysFields(false);
res.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2023 07:20 PM
Try to limit the query to 1 or 2 records only for testing,
After testing if you got the expected result then only add full query to update your records.
Highlighted some changes in bold:
var res = new GlideRecord('sn_sec_cmn_src_ci');
res.addQuery('field_name','value');
res.setLimit(); // set limit as per your choice
res.query();
while(res.next()){
var result =res.source_data;
var stResult = JSON.stringify(result);
var newResult = stResult.replace( /\'\'/g,'');
var parser = JSON.parse(newResult);
var repository = parser.repository;
var newObj = {
repoData : {},
}
newObj.repoData = JSON.parse(repository);
var id = newObj.repoData.id;
var name = newObj.repoData.name;
res.setValue('u_repository_id',id);
res.setValue('u_repository_name',name);
res.setWorkflow(false);
res.autoSysFields(false);
res.update();
}
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates