This is an old revision of the document!
Webex Connect JavaSscript Examples
Convert Google Sheets API response to Correct JSON format (Headers -> Variable Names)
Retrieve Google Sheets worksheet- example:
- https://sheets.googleapis.com/v4/spreadsheets/<google-spreadsheet-id>/values/<sheet-pagename>?key=<your-google-api-key>
Then set the response Code and Response Body as variables when leaving the HTTP node - as per below.
An example output from the Google Sheets API:
{ "range": "customers!A1:Z1000", "majorDimension": "ROWS", "values": [ [ "PhoneNumber", "FirstName", "LastName", "email" ], [ "35315551234", "Bob", "Bobster", "[email protected]" ], [ "353871231234", "Alice", "Anderson", "[email protected]" ] ] }
Format what we want the above to be in
[ { "PhoneNumber": "3535551234", "FirstName": "Bob", "LastName": "Bobster", "email": "[email protected]" }, { "PhoneNumber": "353871231234", "FirstName": "Alice", "LastName": "Anderson", "email": "[email protected]" } ]
Code to Convert to 'correct' format
if (statusCode == 200) { const data = JSON.parse(responseBody); const inputArray = data["values"]; const headers = inputArray[0]; const rows = inputArray.slice(1); const output = []; for (let i = 0; i < rows.length; i++) { let rowObject = {}; // Create a new object for each row - note 'const' should work here - but didn't. Platform using old JS? for (let j = 0; j < headers.length; j++) { rowObject[headers[j]] = rows[i][j]; } output.push(rowObject); // Push the new object to the output array } myheaders = headers.toString(); myrows = rows.toString(); myoutput = output; customersJSON = JSON.stringify(output); 1; } else { 2; }