How to read and save JSON data in an electron APP

Overall idea

  • Turn json data into string and save it to a text file.
  • The text file shoud be placed in the current application user's own directory.

Code example

const { app } = require('electron')
const fs = require('fs')
const path = require('path')

const filePath = path.join(app.getPath('userData'), 'data.json')

function saveData(data) {
    const text = JSON.stringify(data)
    fs.writeFile(filePath, text, err => {
        if (err) {

function readData() {
    fs.readFile(filePath, 'utf8', (err, data) => {
        if (err) {
        const data = JSON.parse(data)
        // handle the json data here

In the example code above, the data is persisted in the current user's userData directory. On windows, the file path looks like C:\Users\<user-name>\AppData\Roaming\<app-name>\data.json.

Posted on 2023-04-22