Files
30-seconds-of-code/snippets/js/s/json-to-file.md
2023-05-07 16:07:29 +03:00

535 B

title, type, language, tags, cover, dateModified
title type language tags cover dateModified
JSON to file snippet javascript
node
travel-mug-3 2020-10-20T23:02:01+03:00

Writes a JSON object to a file.

  • Use fs.writeFileSync(), template literals and JSON.stringify() to write a json object to a .json file.
const fs = require('fs');

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