Files
30-seconds-of-code/snippets/JSONToFile.md
2022-12-04 22:20:49 +02:00

550 B

title, tags, cover, firstSeen, lastUpdated
title tags cover firstSeen lastUpdated
JSON to file node blog_images/travel-mug-3.jpg 2017-12-17T17:55:51+02:00 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'