Issue
We use a handy JSON
module called gsjson to convert Google Sheet data to JSON. I need to get a sheet listed as a list of URLs in TXT format instead.
I have tried changing the extension, but it just outputs as JSON format. How can I convert this into a list of plain text (excluding the header), listing the URLs, line by line?
// Create JSON file from Google Sheet and place in tmp
gsjson({
spreadsheetId: sheetid ,
credentials: 'mycreds.json'
})
.then(function(result) {
result = JSON.stringify(result);
fs.writeFile("/tmp/" + company +".json", result, 'utf-8', function (err) {
if (err) console.log('Google Sheet written to tmp JSON - FAILED'); // an error occurred
else console.log('Google Sheet written to tmp JSON - SUCCESS'); // successful response
});
Current JSON Output
[{"url":"https://example.com"},{"url":"https://examples.com"}]
Desired TXT Output
https://example.com
https://examples.com
Solution
You don't want to stringify
it, that'll convert it to JSON format, which doesn't make sense for what you're looking for. If you want just the url
property from each item of the array, use .map
to extract each property, and then join by newlines:
// ...
.then(function(result) {
const textToWrite = result.map(({ url }) => url).join('\n');
fs.writeFile("/tmp/" + company +".json", textToWrite, 'utf-8', function (err) {
// ...
Answered By - CertainPerformance
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.