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

Is the information on multiple users added to a List (GlideList) element?

Maybe add a screenshot of the form you're trying to generate the data from and highlight the field(s) you are trying to get the data from.

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"
        }
    ]
}