Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

How to store objects in HTML5 localStorage/sessionStorage

I'd like to store a JavaScript object in HTML5 localStorage, but my object is apparently being converted to a string.

I can store and retrieve primitive JavaScript types and arrays using localStorage, but objects don't seem to work. Should they?

Here's my code:

var testObject = { 'one': 1, 'two': 2, 'three': 3 };
console.log('typeof testObject: ' + typeof testObject);
console.log('testObject properties:');
for (var prop in testObject) {
 console.log(' ' + prop + ': ' + testObject[prop]);
}
// Put the object into storage
localStorage.setItem('testObject', testObject);
// Retrieve the object from storage
var retrievedObject = localStorage.getItem('testObject');
console.log('typeof retrievedObject: ' + typeof retrievedObject);
console.log('Value of retrievedObject: ' + retrievedObject);

The console output is

typeof testObject: object
testObject properties:
 one: 1
 two: 2
 three: 3
typeof retrievedObject: string
Value of retrievedObject: [object Object]

It looks to me like the setItem method is converting the input to a string before storing it.

I see this behavior in Safari, Chrome, and Firefox, so I assume it's my misunderstanding of the HTML5 Web Storage specification, not a browser-specific bug or limitation.

I've tried to make sense of the structured clone algorithm described in 2 Common infrastructure . I don't fully understand what it's saying, but maybe my problem has to do with my object's properties not being enumerable (???).

Is there an easy workaround?


Update: The W3C eventually changed their minds about the structured-clone specification, and decided to change the spec to match the implementations. See 12111 – spec for Storage object getItem(key) method does not match implementation behavior . So this question is no longer 100% valid, but the answers still may be of interest.

Answer*

Draft saved
Draft discarded
Cancel
8
  • 207
    do observe that any metadata will be removed. you just get an object with the key-value pairs, so any object with behaviour need to be rebuilt. Commented Oct 7, 2013 at 18:48
  • 7
    @CMS can setItem throw some exception if the data is over the capacity ? Commented Mar 26, 2014 at 9:27
  • 3
    ... applies to objects with circular references only, JSON.stringify() expands the referenced object to its full "content" (implicitly stringified) in the object we stringify. See: stackoverflow.com/a/12659424/2044940 Commented Jul 23, 2014 at 16:54
  • 3
    The problem with this approach are performance issues, if you have to handle large arrays or objects. Commented Oct 29, 2014 at 9:35
  • 4
    @oligofren true, but as maja correctly suggested eval() => , this is one of the good use of , you can easily retrieve function code => store it as string and then eval() it back :) Commented Jul 6, 2015 at 20:03

default

AltStyle によって変換されたページ (->オリジナル) /