Skip to main content Accessibility Feedback

JSON

Unless otherwise noted, these work in all modern browsers, and IE9+. You can extend support back further with polyfills.

JSON.stringify()

Converts objects and arrays into strings.

// A lunch order
var lunch = {
	sandwich: 'turkey',
	chips: 'Cape Cod',
	drink: 'Pepsi'
};

// Save data to local storage
localStorage.setItem('lunchOrder', JSON.stringify(lunch));

// Drink options
var drinks = ['Pepsi', 'water', 'lemonade'];

// Save data to local storage
localStorage.setItem('drinkOptions', JSON.stringify(drinks));

Works back to IE8.

JSON.parse()

Converts stringified JSON back into an object or array.

// Get data from local storage
var savedLunch = JSON.parse(localStorage.getItem('lunchOrder'));
var savedDrinks = JSON.parse(localStorage.getItem('drinkOptions'));

Works back to IE8.