How to convert Json data to string using suitescript2.0

Hi!

I have created script to read excel file data. I am getting data as array of objects format. How to read that data in string format. Please Help!

Below is my code:

/**
* @NApiVersion 2.1
* @NAmdConfig ./readexcelconfig.json
*@NScriptType Suitelet
* … more options …
*/
define([‘N/file’, ‘xlsx’], function(file,XLSX) {
function onRequest(context){
log.debug(“XLSX”,XLSX);
// … use XLSX here …

let arr = []
try {

let f = file.load({ id: 3984316 });
let workbook = XLSX.read(f.getContents(), {type: ‘base64′, cellDates: true,raw: false, dateNF:’dd/mm/yyyy;@’});
log.debug(“workbook”,workbook);

workbook.SheetNames.forEach( ( sheetName ) => {
// Here is your object
let XL_row_object = XLSX.utils.sheet_to_row_object_array( workbook.Sheets[ sheetName ] );
let json_object = JSON.stringify( XL_row_object );
log.debug(“json_object”,json_object);

arr.push( json_object );
} );

log.debug(‘arr’,arr)
const parsed = JSON.parse(arr);
log.debug(“parsed”,parsed);

} catch (error) {
log.debug(‘error’,error)
}
}
return{
onRequest:onRequest
}
})

 

I am getting output as:

TITLE parsed–>

[{” First Header “:”Row 1 Column 1″,” Second Header “:”Row 1 Column 2″,” Third Header “:”Row 1 Column 3″,” Fourth Header “:”Row 1 Column 4″,” Fifth Header “:”Row 1 Column 5″},{” First Header “:”Row 2 Column 1″,” Second Header “:”Row 2 Column 2″,” Third Header “:”Row 2 Column 3″,” Fourth Header “:”Row 2 Column 4″,” Fifth Header “:”Row 2 Column 5”}]

 

Expected Output:

First Header Row 1 Column 1,Second Header Row 1 Column 2,Third Header Row 1 Column 3,Fourth Header Row 1 Column 4,Fifth Header Row 1 Column 5,First Header Row 2 Column 1,Second Header Row 2 Column 2,Third Header Row 2 Column 3,Fourth Header Row 2 Column 4,Fifth Header Row 2 Column 5

Beginner Asked on February 17, 2023 in SuiteScript.
Add Comment
1 Answer(s)

try something like:


let output = '';

const parsed = [

{

"First Header": "Row 1 Column 1",

"Second Header": "Row 1 Column 2",

"Third Header": "Row 1 Column 3",

"Fourth Header": "Row 1 Column 4",

"Fifth Header": "Row 1 Column 5"

},

{

"First Header": "Row 2 Column 1",

"Second Header": "Row 2 Column 2",

"Third Header": "Row 2 Column 3",

"Fourth Header": "Row 2 Column 4",

"Fifth Header": "Row 2 Column 5"

}

]

for (const p of parsed) {

for (const headerKey in p) {

output += `${headerKey} ${p[headerKey]},`

}

}

Intermediate Answered on February 17, 2023.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.
  • This site made possible by our sponsors:   Tipalti   Celigo   Become a Sponsor   Become a Sponsor