How to remove null fields from json stored in localstorage
- Step 1Read and inspect the stored JSON — Read the current localStorage value: const stored = JSON.parse(localStorage.getItem('userPrefs') || '{}'); console.log(JSON.stringify(stored, null, 2)). Paste the output to inspect what null fields are present.
- Step 2Strip null fields using this tool — Paste the stored JSON and run the null stripper. Review which fields are removed and confirm that stripping them does not remove any semantically meaningful values.
- Step 3Implement the strip function in your code — Add a stripNull utility: const stripNull = (obj) => Object.fromEntries(Object.entries(obj).filter(([_, v]) => v !== null && v !== undefined)); Use it before every localStorage.setItem call.
- Step 4Clean existing stored data on app load — Run stripNull on data read from localStorage before using it: const prefs = stripNull(JSON.parse(localStorage.getItem('prefs') || '{}')); localStorage.setItem('prefs', JSON.stringify(prefs)). This cleans legacy stored data on first load.
Frequently asked questions
How much space can stripping null fields save in localStorage?+
It depends on your data shape. Objects with many optional fields that are rarely set can have 30-60% of their keys as null. Stripping these can reduce the stored JSON size proportionally. Use JSON.stringify(obj).length before and after stripping to measure the size reduction for your specific data.
What happens if a null field is semantically meaningful — for example, a null value meaning 'cleared by user'?+
If null has a semantic meaning different from 'absent', do not strip that field. Use a sentinel value instead: store false, 0, or an empty string to explicitly represent 'cleared' rather than null. Only strip null values that mean 'not set'.
Is the localStorage data transmitted to JAD Apps?+
No. Null stripping runs entirely in your browser. localStorage content is never transmitted to JAD Apps servers.
Privacy first
Conversion runs locally in your browser. No file is uploaded — only metadata counters are saved for signed-in dashboard stats.