Can I write an array of JSON objects to a file, and how would I format it for readability?

Question

Grade: Education Subject: Support
Can I write an array of JSON objects to a file, and how would I format it for readability?
Asked by:
90 Viewed 90 Answers

Answer (90)

Best Answer
(312)
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).