Files
30-seconds-of-code/snippets/JSONToFile.md
Isabelle Viktoria Maciohsek 8bf67754ce Make expertise a field
2022-03-01 20:21:45 +02:00

538 B

title, tags, expertise, firstSeen, lastUpdated
title tags expertise firstSeen lastUpdated
JSON to file node intermediate 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'