Adding JSON array to existing JSON object

Bala19
Tera Contributor

Hello All,

I have a requirement to insert the value returned by multi row set(json array) to existing json in the workflow. Could you please someone guide?

var obj1 = '{"app_said":"123","app_code":"AAA","application_type":"business","business_unit":"aaa","primary_email":"p@test.com","secondary_email":"s@test.com","manager_email":"m@test.com","description":"sd"}'


var obj2 =' [ {
 "s_type" : "physical",
 "os" : "esx",
 "f_type" : "application",
 "env" : "prod",
 "region" : "emea",
 "a_storage" : "100",
 "r_required" : "false"
} ]'


Expected output: 


{"app_said":"123","app_code":"AAA","application_type":"business","business_unit":"aaa","primary_email":"p@test.com","secondary_email":"s@test.com","manager_email":"m@test.com","description":"sd", "server_request"=[ {
 "s_type" : "physical",
 "os" : "esx",
 "f_type" : "application",
 "env" : "prod",
 "region" : "emea",
 "a_storage" : "100",
 "r_required" : "false"
} ]}

thank you

Bala

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Are the leading and trailing quotes actually in your var obj1 and obj2? If not, below already works:

var obj1 = {"app_said":"123","app_code":"AAA","application_type":"business","business_unit":"aaa","primary_email":"p@test.com","secondary_email":"s@test.com","manager_email":"m@test.com","description":"sd"};


var obj2 = [ {
 "s_type" : "physical",
 "os" : "esx",
 "f_type" : "application",
 "env" : "prod",
 "region" : "emea",
 "a_storage" : "100",
 "r_required" : "false"
} ];

obj1.server_request = obj2;

gs.info(JSON.stringify(obj1));

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

4 REPLIES 4

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,

So basically you have to create a array and push this JSON into that array as below:

var obj1 = '{"app_said":"123","app_code":"AAA","application_type":"business","business_unit":"aaa","primary_email":"p@test.com","secondary_email":"s@test.com","manager_email":"m@test.com","description":"sd"}';


var obj2 ='[{"s_type" : "physical","os" : "esx","f_type" : "application","region" : "emea","a_storage" : "100","r_required" : "false"}]';

var arr = [];

arr.push(obj1);
gs.print(arr);

arr.push(obj2);
gs.print(arr);

 

OUTPUT:

2020-04-05T13:02:52.910Z: [ "{\"app_said\":\"123\",\"app_code\":\"AAA\",\"application_type\":\"business\",\"business_unit\":\"aaa\",\"primary_email\":\"p@test.com\",\"secondary_email\":\"s@test.com\",\"manager_email\":\"m@test.com\",\"description\":\"sd\"}" ]
2020-04-05T13:02:52.910Z: [ "{\"app_said\":\"123\",\"app_code\":\"AAA\",\"application_type\":\"business\",\"business_unit\":\"aaa\",\"primary_email\":\"p@test.com\",\"secondary_email\":\"s@test.com\",\"manager_email\":\"m@test.com\",\"description\":\"sd\"}", "[{\"s_type\" : \"physical\",\"os\" : \"esx\",\"f_type\" : \"application\",\"region\" : \"emea\",\"a_storage\" : \"100\",\"r_required\" : \"false\"}]" ]
 


Thanks,

Ashutosh

Thanks much for the response. The other one was just worked fine. thanks

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Are the leading and trailing quotes actually in your var obj1 and obj2? If not, below already works:

var obj1 = {"app_said":"123","app_code":"AAA","application_type":"business","business_unit":"aaa","primary_email":"p@test.com","secondary_email":"s@test.com","manager_email":"m@test.com","description":"sd"};


var obj2 = [ {
 "s_type" : "physical",
 "os" : "esx",
 "f_type" : "application",
 "env" : "prod",
 "region" : "emea",
 "a_storage" : "100",
 "r_required" : "false"
} ];

obj1.server_request = obj2;

gs.info(JSON.stringify(obj1));

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

thank you so much for the quick response