Skip to content

Instantly share code, notes, and snippets.

@andrit
Created July 10, 2018 16:27
Show Gist options
  • Save andrit/ee64dc2e7bf3fcdbf0fdbfb847bb0621 to your computer and use it in GitHub Desktop.
Save andrit/ee64dc2e7bf3fcdbf0fdbfb847bb0621 to your computer and use it in GitHub Desktop.

Write a JSON to a file in Node

Use fs.writeFile(), template literals and JSON.stringify() to write a json object to a .json file.

const fs = require('fs');
const jsonToFile = (obj, filename) => fs.writeFile(`${filename}.json`, JSON.stringify(obj, null, 2))
// jsonToFile({test: "is passed"}, 'testJsonFile') -> writes the object to 'testJsonFile.json'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment