CodShot
JavaScript Public

Save and Load JSON from localStorage

A safe small helper for storing JSON data in localStorage.

#javascript #localstorage #json #utility
JavaScript
function saveJson(key, value) {
  localStorage.setItem(key, JSON.stringify(value));
}

function loadJson(key, fallback = null) {
  try {
    const raw = localStorage.getItem(key);
    return raw ? JSON.parse(raw) : fallback;
  } catch (error) {
    console.error('Invalid stored JSON:', error);
    return fallback;
  }
}

saveJson('settings', { theme: 'dark' });

const settings = loadJson('settings', { theme: 'light' });
console.log(settings);

Notes

Never store passwords or sensitive account data in localStorage.

Snippet actions

CodShot user
CodShot user
Updated: 2026-06-03 14:05
Public snippets
0
Forks
CodShot AI Help
Ask about CodShot features, plans, workspace, AI limits, referrals, and public help topics.
Hi! I can help you understand how CodShot works. What would you like to know?
For account-specific or sensitive issues, please open a support ticket. Open support