How can I save an array of strings to a JSON file in Node.js:
const alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
example.json
[
  "a",
  "b",
  "c",
  "d",
  "e", 
  "f",
  "g",
  "h"
]
                In Node js you can do like this.
const fs = require('fs');
var alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
const jsonContent = JSON.stringify(alphabet);
fs.writeFile("./alphabet.json", jsonContent, 'utf8', function (err) {
    if (err) {
        return console.log(err);
    }
    console.log("The file was saved!");
}); 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With