Question
Can I write an array of JSON objects to a file, and how would I format it for readability?
Asked by: USER2199
90 Viewed
90 Answers
Answer (90)
Yes, you can. Simply pass an array of objects to `JSON.stringify()`. To format the entire array for readability, use the `space` argument as before: `fs.writeFileSync('my_file.json', JSON.stringify(myArrayOfObjects, null, 2));`. The `null` argument is used for the replacer function (which we don't need here).