Nest all content into snippets
This commit is contained in:
24
snippets/js/s/json-to-file.md
Normal file
24
snippets/js/s/json-to-file.md
Normal file
@ -0,0 +1,24 @@
|
||||
---
|
||||
title: JSON to file
|
||||
type: snippet
|
||||
language: javascript
|
||||
tags: [node]
|
||||
cover: travel-mug-3
|
||||
dateModified: 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'
|
||||
```
|
||||
Reference in New Issue
Block a user