Files
30-seconds-of-code/snippets/write-json-to-file.md
Angelos Chalaris ad155b8831 Build
2017-12-16 13:03:46 +02:00

364 B

Write JSON to file

Use fs.writeFile(), template literals and JSON.stringify() to write a json object to a .json file.

const fs = require('fs');
const jsonToFile = (obj, filename) => fs.writeFile(`${filename}.json`, JSON.stringify(obj, null, 2))
// jsonToFile({test: "is passed"}, 'testJsonFile') -> writes the object to 'testJsonFile.json'