How to get the value of repository id and Name from the source data field by using array?

jugantanayak
Tera Guru

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",

2 ACCEPTED SOLUTIONS

Hi @jugantanayak 

 

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 : 

 

VishalBirajdar_0-1695699571048.png

 

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

View solution in original post

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.

var res = new GlideRecord('sn_sec_cmn_src_ci');
res.addQuery("source","57fd7e2353b300100ac3ddeeff7b1233");
res.addNotNullQuery("source_data");
res.setLimit(3);
res.query();
while(res.next()){
var result =res.getValue('source_data');
var newResult = result.replace( /\'\'/g,'');
var parser = JSON.parse(newResult);
var repository = parser.repository;
var newObj = {};
newObj = JSON.parse(repository);
var idRepo =newObj.id;
var nameRepo = newObj.name;
res.u_repository_id=idRepo;
res.u_repository_name=nameRepo;
res.update();

}

View solution in original post

21 REPLIES 21

Hi @Vishal Birajdar ,

Fix script didn't work. I am sharing the code can you please suggest.

 

var res = new GlideRecord('sn_sec_cmn_src_ci');
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();
}
Note: // res.addQuery('source_data); // Removed this line as didn't find any argument in it and it's a description type field and i need all the data mentioned inside this field.

@jugantanayak 

 

Can you share the screenshot of form ? Especially the want to see the " source data" field.

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Hi @Vishal Birajdar ,

I am sharing screenshot as attached to this post.

 

Regards,

Juganta

 

@jugantanayak 

 

See the updated script : Highlighted changes in green & line which is not require commented & in red

 

var res = new GlideRecord('sn_sec_cmn_src_ci');

res.setLimit(100);  // You have to put number here (how many records you want to limit)
& cant be empty .If you want all records to b updated then remove this line

res.query();

while(res.next()){
var result =res.getValue('source_data');   //Use backend name of Source data field

// var stResult = JSON.stringify(result);  //not require as its already a string 

var newResult = result.replace( /\'\'/g,'');

var parser = JSON.parse(newResult);
var repository = parser.repository;

var newObj = {
repoData : {},
};

newObj.repoData = JSON.parse(repository);
var idRepo = newObj.repoData.id;
var nameRepo = newObj.repoData.name;
res.setValue('u_repository_id',idRepo);
res.setValue('u_repository_name',nameRepo);

res.setWorkflow(false);
res.autoSysFields(false);

res.update();

}

 

Output : 

For me , I have created a fields in "sn_customerservice_case" for testing

See the output in attachment.

 

Please mark the Answer as Correct if this helps you....!!

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Hi @Vishal Birajdar ,

 

Currently I am getting below mentioned error.

Evaluator: com.glide.script.RhinoEcmaError: Unexpected token: u
script : Line(12) column(0)
9: var newObj = {
10: repoData : {},
11: }
==> 12: newObj.repoData = JSON.parse(repository);
13: var idRepo = newObj.repoData.id;
14: var nameRepo = newObj.repoData.name;
15: res.setValue('u_repository_id',idRepo);

[0:00:00.040] Total Time

 

Regards,

Juganta