how to create nested json dynamically and have to use REST message

String
Kilo Sage

Hi team,

we are using BR to fetching the incident record values, but unable  to create a nested JSON pattern as below

{
"requestor": "test",
"email": "abc@xyz.com",

"Details": [{
"name": "test",
"age": "New",
"place": "",
"action": "Add",
"city": ["ABC", "XYZ"]},
{"name": "yes",
"age": "New",
"place": "hello",
"action": "Add",
"city": ["ABC", "XYZ"]}
]}

 Where Details will dynamically add, depends on users info and city attribute is an array and how the REST message should set to the above JSON 

Any suggestions, please 

1 ACCEPTED SOLUTION

If you are working with a GlideList field, the following is an example that may help you

var obj = {
	requestor: current.getDisplayValue('caller_id'),
	email: current.caller_id.email.toString(),
	Details: []
};

var userGR = new GlideRecord('sys_user');
userGR.addQuery('sys_id', 'IN', current.getValue('watch_list'));
userGR.query();

while(userGR.next()) {
	obj.Details.push({
		name: userGR.getValue('user_name')
	});
}

Which would generate a JSON that looks like

{
    "requestor": "Abel Tuter",
    "email": "abel.tuter@example.com",
    "Details": [
        {
            "name": "adela.cervantsz"
        },
        {
            "name": "aileen.mottern"
        },
        {
            "name": "abraham.lincoln"
        }
    ]
}

View solution in original post

6 REPLIES 6

Callum Ridley1
Tera Guru

Please could you show what you have tried so far? I don't see why there would be an issue generating the JSON you have shown an example of.

Hi Callum,

Thanks for your quick reply ,Please check the below script 

var requestJSON = {
"requestor": "" + current.req,
"email": "" + current.user_mail,
"name": "" + current.user_name,
"age": "" +current.user_age,
"place": "" +current.user_place,
"action": "" +current.place,
"city": "" +current.city
};
var r = new sn_ws.RESTMessageV2('outbound', 'INC');
r.setRequestHeader("Accept", "application/json");
r.setRequestBody(JSON.stringify(requestJSON));

var response = r.execute();

Iam using the above script to create the json ,but iam getting normal json 

how to create  the above json where Details attribute  will add dynamically expand 

What are you expecting the fill the Details array with? Your business rule is only looking at one record (the current record), but it looks like you want to include data from multiple records?

What are the trigger conditions for your business rule?

Multiple user info will insert into incident form, So  need to fetch user info and add to JSON in details attributes dynamically and the city is multiple places, So have to  array 

trigger conditions for your business rule: when a new record inserted