Create a json file by writing a JSON object to the filesystem

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'

Tags: writeFileSync, filesystem, fs, json, stringify, json object, Node.js, Node, Javascript, Typescript

CC BY 4.0 added intro and tags – 30 Seconds of Code