Writing JSON object to a JSON file with fs.writeFileSync

Jsonnode.jsFileFs

Json Problem Overview


I am trying to write a JSON object to a JSON file. The code executes without errors, but instead of the content of the object been written, all that gets written into the JSON file is:

[object Object]

This is the code that actually does the writing:

fs.writeFileSync('../data/phraseFreqs.json', output)

'output' is a JSON object, and the file already exists. Please let me know if more information is required.

Json Solutions


Solution 1 - Json

You need to stringify the object.

fs.writeFileSync('../data/phraseFreqs.json', JSON.stringify(output));

Solution 2 - Json

I don't think you should use the synchronous approach, asynchronously writing data to a file is better also stringify the output if it's an object.

Note: If output is a string, then specify the encoding and remember the flag options as well.:

const fs = require('fs');
const content = JSON.stringify(output);

fs.writeFile('/tmp/phraseFreqs.json', content, 'utf8', function (err) {
    if (err) {
        return console.log(err);
    }

    console.log("The file was saved!");
}); 

Added Synchronous method of writing data to a file, but please consider your use case. Asynchronous vs synchronous execution, what does it really mean?

const fs = require('fs');
const content = JSON.stringify(output);

fs.writeFileSync('/tmp/phraseFreqs.json', content);

Solution 3 - Json

Make the json human readable by passing a third argument to stringify:

fs.writeFileSync('../data/phraseFreqs.json', JSON.stringify(output, null, 4));

Solution 4 - Json

When sending data to a web server, the data has to be a string (here). You can convert a JavaScript object into a string with JSON.stringify(). Here is a working example:

var fs = require('fs');

var originalNote = {
  title: 'Meeting',
  description: 'Meeting John Doe at 10:30 am'
};

var originalNoteString = JSON.stringify(originalNote);

fs.writeFileSync('notes.json', originalNoteString);

var noteString = fs.readFileSync('notes.json');

var note = JSON.parse(noteString);

console.log(`TITLE: ${note.title} DESCRIPTION: ${note.description}`);

Hope it could help.

Solution 5 - Json

Here's a variation, using the version of fs that uses promises:

const fs = require('fs');

await fs.promises.writeFile('../data/phraseFreqs.json', JSON.stringify(output)); // UTF-8 is default

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRomulus3799View Question on Stackoverflow
Solution 1 - JsonKamalView Answer on Stackoverflow
Solution 2 - JsonAkinjideView Answer on Stackoverflow
Solution 3 - JsonTimelotView Answer on Stackoverflow
Solution 4 - JsonReza Baradaran GazorisangiView Answer on Stackoverflow
Solution 5 - JsonFlimmView Answer on Stackoverflow