To write JSON object to a JSON file with Node.js fs.writeFileSync, we call JSON.stringify
.
For instance, we write
const fs = require("fs");
const content = JSON.stringify(output);
fs.writeFileSync("/tmp/phraseFreqs.json", content);
to call JSON.stringift
to coinvert the output
object to a JSON string.
Then we call writeFileSync
with the path to write to and the stringified JSON object content
to write the JSON to the file.