Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving JSON in Electron

I am building an app using Electron. In this app, I am building a data structure using JSON. My data structure looks like this:

{
  items: [
    { id:1, name:'football' },
    { id:2, name:'soccer ball' },
    { id:3, name:'basketball' }
  ]
}

I want to save this JSON to a file called "data.json". I want to save it to a file because I want to load the next time the application starts. My challenge is, I do not know how to save the data. In fact, I'm not sure where I should even save the file. Do I save it in the same directory as the app? Or is there some cross-platform approach I should use?

Currently, I have the following:

saveClick: function() {
  var json = JSON.stringify(this.data);
  // assume json matches the JSON provided above.
  // Now, I'm not sure how to actually save the file.
} 

So, how / where do I save JSON to the local file system for use at a later time?

like image 932
JQuery Mobile Avatar asked Oct 22 '15 19:10

JQuery Mobile


People also ask

How to save data in electelectron?

Electron saves data in app.getPath ("userData") folder, which is different in every os. electron-storage gives simple methods to get and set json files to this directory. Creates subdirectories if needed - that means you can write movies/StarWars.json as path, a movies folder will be created and a StarWars.json file inside.

Where does electron save its data?

Merge remote-tracking branch 'upstream/master' into feature/use-ya-st… Electron saves data in app.getPath ("userData") folder, which is different in every os. electron-storage gives simple methods to get and set json files to this directory.

Is there any module to let electron save files?

Is there any module I can use to let Electron save files locally? Show activity on this post. If you are targeting multiple platforms, I answered a similar question here. Basically app.getPath (name), app.setPath (name, path), and app.getAppPath () are very useful in saving files to the the right place regardless of the OS.

How do I save data from a renderer to a JSON file?

If we want to access that data easily from both the renderer and main, using the same API, we can use the node.js fs(file system) module to save data to a JSON file (or any format we want). This approach covers the most common use cases, so we’ll cover how to do this in an example in a moment.


2 Answers

Electron lacks an easy way to persist and read user settings for your application. electron-json-storage implements an API somehow similar to localStorage to write and read JSON objects to/from the operating system application data directory, as defined by app.getPath('userData').

like image 83
Bruno Avatar answered Oct 03 '22 19:10

Bruno


I wrote a simple library that you can use, with a simple interface, it also creates subdirectories and works with promises/callbacks. it will save the data into app.getPath("appData") as the root folder.

https://github.com/ran-y/electron-storage

Installation

$ npm install --save electron-storage

usage

const storage = require('electron-storage');

API

storage.get(filePath, (err, data) => {
  if (err) {
    console.error(err)
      } else {
        console.log(data);
      }
    });

storage.get(filePath)
    .then(data => {
      console.log(data);
    })
    .catch(err => {
      console.error(err);
    });

storage.set(filePath, data, (err) => {
      if (err) {
        console.error(err)
      }
    });

storage.set(filePath, data)
    .then(data => {
      console.log(data);
    })
    .catch(err => {
      console.error(err);
    });
like image 30
Ran Yitzhaki Avatar answered Oct 03 '22 20:10

Ran Yitzhaki