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
4
  • 1
    This is a brilliant resource and just what I need. I'm doing Ionic apps with AngularJS where I need to save certain javascript objects in localStorage and up to this point I've just been doing JSON.parse and JSON.stringify, and they work, but it's a bit more cumbersome than being able to just use a utility like this one. I'm going to try it. Commented Jan 21, 2018 at 3:30
  • No offense, but your lib seems like overkill for 99% use cases. And to not even mention the lighter approach (e.e. JSON.stringify/parse) or alternative libraries seems misleading to me. Commented Oct 2, 2023 at 16:52
  • Sorry, let me be a bit more constructive/positive: This answer could be improved by comparing against alternatives approaches. Why and when does your library best alternatives? Commented Oct 2, 2023 at 16:57
  • Misleading? No, not really. I'm not on the hook to mention other libraries. The answers directly above and below mention several libraries: I'm sure they're all worthy. JSON.stringify is cited plentifully on this page; no need for me to be redundant about it. My library does some things most others don't, but I don't expect anyone to consider it unless they need it (I know I wouldn't). Beyond that, it's unclear why you're critical of something because it does more than bare minimum. I'd be wasting time offering yet another "me too" solution. Commented Oct 11, 2023 at 20:07

default

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