JSON to file

JavaScript, Node · Oct 20, 2020

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'

More like this

  • Encode string to Base64

    Creates a base-64 encoded ASCII string from a String object in which each character in the string is treated as a byte of binary data.

    JavaScript, Node · Sep 15, 2020

  • Create a static file server with Node.js

    Create your own static file server with Node.js in just 70 lines of code.

    JavaScript, Node · Jun 5, 2022

  • Introduction to the Node.js test module

    The Node.js test module is a new testing tool that's still in its early stages. Learn more about it in this short introduction.

    JavaScript, Node · Apr 30, 2023