I’m getting a result dynamically using a for loop ...
# suitescript
n
I’m getting a result dynamically using a for loop like:
Copy code
{
  "results": [
    {
      "key1": "SO9005383"
    },
    {
      "key2": "11/3/2021"
    },
    {
      "key3": "Abhi"
    },
    {
      "key4": "Rohit"
    },
    {
      "key5": "USD"
    },
    {
      "key1": "SO9005387"
    },
    {
      "key2": "14/3/2021"
    },
    {
      "key3": "Akash"
    },
    {
      "key4": "Ajay"
    },
    {
      "key5": "INR"
    }
  ]
}
I tried Object assign function but not working I want the above JSON like Below:
Copy code
{
 	[

 		{
 				"key1": "SO9005383",
 				"key2": "11/3/2021",
 				"key3": "Abhi",
 				"key4": "Rohit",
 				"key5": "USD"
 		},
 		{
 				"key1": "SO9005387",
 				"key2": "14/3/2021",
 				"key3": "Akash",
 				"key4": "Ajay",
 				"key5": "INR"
 		}

 	]
 }
Kindly help me here guys!! Thanks in advance!
b
not possible
you cant have duplicate keys
n
Actually , I have different keys!! can you please just let me know for the values!! please ignore the keys
I have just edited the JSON, can you please check that Thanks
b
keys are actually going to be pretty important
from what you shared, the nicest way to start a new object is when you see key1 again
shouldnt be an issue in this case, its an array of objects with 1 key
m
sorry ignore me
n
@battk I’ll share my code here! I want to get the Keys and values from the search/JSON but right now i’m able to get the values only that also like I shown in first JSON above!! but I want it as shown in 2nd JSON , but I don’t have keys there yet!!
Below is my code!! could you please let me know how I can get it! if you please help me here! that will be very helpful🙂
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 */

define(['N/ui/serverWidget', 'N/record', 'N/search', 'N/log', 'N/runtime', 'N/file', 'N/encode','N/format','N/url'],function(serverWidget, record, search, log, runtime, file, encode,format,url){

  function onRequest(context) {
      
      log.debug('testing123',JSON.stringify(context.request.body));
      
      if(context.request.method === 'POST') {
        
        var body = {};
        
        body = JSON.parse(context.request.body);
        
        
        log.debug('welcome to the suitelet!!',body.searchID);

        
        log.debug('testing123567');
            try {
  
                  if ( ( body.searchID == 'undefined' ) || ( body.searchID === null ) || ( body.searchID == '' ) ) { 

                    throw { 'type': 'error.SavedSearchAPIError', 'name': 'INVALID_REQUEST', 'message': 'No searchID was specified.' }     
                  
                  } 


              
                  log.debug('testing123567890');

                  var searchObj = search.load( { id: body.searchID } );

                  var col = JSON.stringify(searchObj.columns);

                 // log.debug('search columns',col);



                  var response = {};
                  response.results = [];

                  var resultSet = searchObj.run();

                  var searchResultCount = searchObj.runPaged().count;
              
                  log.debug('result set', resultSet);

                  var start = 0;

                  var results = [];


                  	var firstResult = searchObj.run().getRange({
							  start: 0,
							  end: 1
							 })[0];


            do {
                

                     results = resultSet.getRange( { start: start, end: start+1000});

                     start = start+1000;

                for(var l = 0;l<results.length;l++){

                	var temp_obj = {};

	                 var resultss = results[l];


                    var column_results = resultss.columns;

                	for (var m = 0; m < column_results.length; m++) {

                    	var label = firstResult.columns[m].label;

                    	log.debug('colllllll label',label);

               
               if(resultss.getText(column_results[m]) == undefined || resultss.getText(column_results[m]) == null || resultss.getText(column_results[m]) == ""){

                 
               	temp_obj = {label:resultss.getValue(column_results[m])};

               	log.debug('temp_obj',temp_obj);


               	response.results = response.results.concat(temp_obj);


                 //response.results = response.results.concat( resultss.getValue(column_results[m]));
                // log.debug('resultSet.getText(column_results[m])',resultss.getText(column_results[m]));

               }else{

               	temp_obj = {label:resultss.getText(column_results[m])};

               	log.debug('temp_obj',temp_obj);


               	response.results = response.results.concat(temp_obj);

                //response.results = response.results.concat(resultss.getText(column_results[m]));
                //log.debug('resultSet.getValue123(column_results[m])',resultss.getText(column_results[m]));

               }
                

             }


            }
                   
                  
        } while ( resultSet && resultSet.length > 0 );
                   
                   log.debug('response',response);

                   var offSet = 100;

                    var next_page_URL = "<https://5454552.extforms.netsuite.com/app/site/hosting/scriptlet.nl?script=1759&deploy=1&compid=5454552&h=e3c5cc8154ade6f19ccb&offSet=>"+offSet+""; 

                    response.next_page_url = next_page_URL;

                    var x = 0,key1 = "key123";
                    response[key1] = x;
                  
                  context.response.write(JSON.stringify(response));
                      
                } catch( e ) {  
                  log.debug( { 'title': 'error', 'details': e } );
                  return { 'error': { 'type': e.type, 'name': e.name, 'message': e.message } }
                }


          
              
  }
  
  }

  return {onRequest: onRequest};
  
});
b
your logic is hurting you here
your results started as the second object, you turned it into the first object
and now you want to turn it back into the second object
do it right the first time
my guess is that you only know how to use literal property names
n
okay THanks @battk