JSON Structure with multiple level

dhavalkshah
Kilo Explorer

I want to build a following structure for JSON to post to REST message.

"approvals": [{

"number": "CHG000003710",

"state": "Requested",

"short_description": "Changes in Test",

"opened_by_employee_number": "XXXXXX",

"opened_by": "ABC",

"approver_employee_number": "XXXXX",

"approver": "PQR",

"assignment_group": "ABCD",

"created_on": "06-28-2014 00:11:21",

"due_date": "06-28-2014 00:11:21",

"url": "google.com"

}],

"total_count": "1",

"detail": {

"details": [{

"change_number": "CHG000003710",

"chg_req_workgroup": "ABCD",

"short_description": "Changes   in Test",

"state": "Requested",

"start_date": "08-21-2015 15:20:43",

"end_date": "08-21-2015 18:20:51",

"type": "Minor",

"risk": "2 - High",

"chg_environment": "Production/DR",

"chg_onhold": "false",

"justification": "",

}],

"total_count": "1"

}

}

Is there any way to achieve that easily ? We are trying to write it by having string and insert it in this format but mutliple level structure is making it difficult to achieve when we have dynamic data.

Thanks

Dhaval

2 REPLIES 2

Jeet
Tera Expert

Hi ,



Use below code If you know about the your data patterns... Create javascript array and then convert it into JSON object.



var myarray = [];


var myJSON = "";


var approvals = {};


var details         = {};


var total_count ="";



for (var i = 0; i < 10; i++) { // Depends on your data length



                approvals.number =       data1;


                approvals.state = data2 ;


                approvals.due_date   = data3 ;


                approvals.url =   data4 ;


                *******************


                **********



  myarray.push(approvals);                                                                         // add items to javaScript array



          total_count = data5;


myarray.push(data5);                                                                                           // add items to javaScript array



                      details.change_number = data6;


                      details.chg_req_workgroup = data7;


                        *********************


                        **************


  myarray.push(details);                                                                                   // add items to javaScript array



}




myJSON = JSON.stringify(myarray);   // convert javaScript array into JSON object


gs.print(myJSON);




Thanks,


Jeet


Alikutty A
Tera Sage

You need to build it up using JSON and arrays. Here is what you can start with, The json you provided is not properly formatted.



var root = {};


var approvals = {};


var approvalArr = [];


var detail = {};


var details = {};


var detailsArr = [];



approvals.number ="";


approvals.state ="";


//Add other attributes


approvalArr.push(approvals);   // Loop as per your dynamic logic



details.change_number ="";


//Add other attributes


detailsArr.push(details);



detail.total_count = "1";


detail.details = details;


root.approvals = approvals;


root.totat_count = "1";


root.details = details;



The code should match the format in which your JSON is structure, let me know if you need further help.



Thank You


Please Hit Like, Helpful or Correct depending on the impact of response