Files
30-seconds-of-code/snippets/JSONToFile.md
Isabelle Viktoria Maciohsek 27c168ce55 Bake date into snippets
2021-06-13 13:55:00 +03:00

23 lines
525 B
Markdown

---
title: JSONToFile
tags: node,intermediate
firstSeen: 2017-12-17T17:55:51+02:00
lastUpdated: 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.
```js
const fs = require('fs');
const JSONToFile = (obj, filename) =>
fs.writeFileSync(`${filename}.json`, JSON.stringify(obj, null, 2));
```
```js
JSONToFile({ test: 'is passed' }, 'testJsonFile');
// writes the object to 'testJsonFile.json'
```