Categories
JavaScript Answers

How to write JSON object to a JSON file with Node.js fs.writeFileSync?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *