# How to write a JSON object to file in Node.js

> Learn how to write a JSON object to a file in Node.js with JSON.stringify() and fs.writeFileSync(), then read it back later using fs.readFileSync().

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2018-10-14 | Topics: [Node.js](https://flaviocopes.com/tags/node/) | Canonical: https://flaviocopes.com/how-to-save-json-object-to-file-nodejs/

Sometimes the best way to store some data in a [Node.js](https://flaviocopes.com/nodejs/) application is to save it to the filesystem.

If you have an object that can be serialized to [JSON](https://flaviocopes.com/json/), you can use the `JSON.stringify()` method and the `fs` method `fs.writeFileSync()` which synchronously writes a piece of data to a file (tip: to check or pretty-print the file you produced, paste it into my [JSON formatter](https://flaviocopes.com/tools/json-formatter/)):

```js
const fs = require('fs')

const storeData = (data, path) => {
  try {
    fs.writeFileSync(path, JSON.stringify(data))
  } catch (err) {
    console.error(err)
  }
}
```

To retrieve the data, you can use `fs.readFileSync()`:

```js
const loadData = (path) => {
  try {
    return fs.readFileSync(path, 'utf8')
  } catch (err) {
    console.error(err)
    return false
  }
}
```

We used a synchronous API, so we can easily return the data once we get it.

We can also decide to use the asynchronous versions, `fs.writeFile` and `fs.readFile`, although the code will change a little bit, and I recommend you take a read at [how to write files using Node.js](https://flaviocopes.com/node-writing-files/) and [how to read files using Node.js](https://flaviocopes.com/node-reading-files/) for this.
