Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Parsing Google Sheet data into JSON - dealing with newline character

$
0
0

In Google Sheets I have cells with text like this: first line\nsecond line

Obviously I want a new line character to be preserved in the json that I'm outputting, but in the json it ends up as:

"text": "first line\\nsecond line"

Here's the Apps Script:

var spreadsheetId = 'someId';  var spreadsheet = SpreadsheetApp.openById(spreadsheetId);  var sheets = spreadsheet.getSheets();  for (var i =  1; i < sheets.length; i++) {    var sheet = sheets[i];    var data = sheet.getRange(3,  1, sheet.getLastRow() -  2, sheet.getLastColumn()).getValues();    var headers = sheet.getRange(2,  1,  1, sheet.getLastColumn()).getValues()[0];    var objects = data.map(function(row) {    var obj = {};    for (var j =  0; j < headers.length; j++) {      var value = (row[j] === '') ? null : row[j];      // If the header is one of the specified ones, convert the value to a string      if (headers[j] === 'prefs' || headers[j] === 'text') {        if (value != null) {          obj[headers[j]] = String(value);        } else {          obj[headers[j]] = value;        }      } else {        // For other headers, assign the value directly        obj[headers[j]] = value;      }    }    return obj;    });     // Convert the array of objects to a JSON string    var json = JSON.stringify(objects, null,  2);    // Create a blob from the JSON string    var blob = Utilities.newBlob(json, 'application/json', sheet.getName() +'.json');    ...

What can I do to preserve the single \n in the json, so that it doesn't add the extra \?


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>