Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing JSON data in browser memory

I want to persist some JSON information in browser. Depending on user interaction with the application, I want to store 5-6 different JSON object into memory. What options I have to achieve this? Please suggest any library or plugin using which I can persist information in the browser.

Thanks

like image 563
SharpCoder Avatar asked Sep 20 '13 09:09

SharpCoder


People also ask

Where can I store JSON data?

You can store JSON documents in SQL Server or SQL Database and query JSON data as in a NoSQL database.

Can you save JSON in local storage?

In summary, we can store JavaScript objects in localStorage by first converting them to strings with the JSON. stringify method, then back to objects with the JSON. parse method.


2 Answers

To add to the solutions given, I'd also want to add a reference link Storing Objects in HTML5 localStorage where this question is discussed nicely.

Below is the code

var testObject = { 'one': 1, 'two': 2, 'three': 3 };

// Put the object into storage
localStorage.setItem('testObject', JSON.stringify(testObject));

// Retrieve the object from storage
var retrievedObject = localStorage.getItem('testObject');

console.log('retrievedObject: ', JSON.parse(retrievedObject));

Courtesy: CMS

like image 86
Rupam Datta Avatar answered Oct 06 '22 04:10

Rupam Datta


You can use HTML5 storage which gives you both local and session storage.

Local storage persists it in a local cache and can therefore be accessed again in the future, despite the browser being closed.

Session storage will only store the information for that particular session and will be wiped once the session ends.

e.g.

//get item from storage
var foo = localStorage["bar"];

//set item in storage.
localStorage["bar"] = foo;
like image 36
BenM Avatar answered Oct 06 '22 05:10

BenM