JSON (JavaScript Object Notation) is the standard text format for exchanging structured data between JavaScript and APIs, files, and storage. This lesson covers converting objects to JSON strings with JSON.stringify() and parsing them back into live JavaScript objects with JSON.parse().
1JS Utilities | JavaScript Tutorial - In-Depth Guide Part 1
JSON is the standard format for exchanging data. Use JSON.stringify() to convert objects to strings, and JSON.parse() to convert strings back to objects.
const json = JSON.stringify({ name: 'Alice' });
const obj = JSON.parse(json);2Step-by-Step Breakdown
JSON is the standard format for exchanging data. Use JSON.stringify() to convert objects to strings, and JSON.parse() to convert strings back to objects.
Level Up š
Advanced cheat sheets, SEO tricks, and interview prep for this topic.
Browser Support
Fully supported.
Fully supported.
Fully supported.
Fully supported.
Accessibility (A11y)
1Structured Data (JSON-LD) Improves How Assistive Search Tools Understand Page Content
JSON.stringify() is what turns a JavaScript object describing your page's content (a Product, FAQ, or Article schema) into the JSON-LD embedded in a page's `<script type="application/ld+json">` tag ā accurate structured data helps voice assistants and other accessible search tools surface your content correctly.
SEO Implications
- 1
JSON.stringify() Is How Structured Data (Schema.org/JSON-LD) Gets Embedded in a Page
Search engines read a page's JSON-LD structured data block to understand entities like products, articles, and FAQs; that block is typically produced by calling JSON.stringify() on a JavaScript object matching the schema.org vocabulary, so a malformed or incomplete object directly produces invalid structured data.
Best Practices
Always Wrap JSON.parse() in a Try/Catch
JSON.parse() throws a SyntaxError on malformed or unexpected input (like an empty string, HTML error page, or truncated response) ā always wrap it in a try/catch when parsing data from an external source like an API response or localStorage, so a bad payload doesn't crash your app.
Use the Replacer and Indent Arguments of JSON.stringify() for Cleaner Output
JSON.stringify(value, replacer, space) accepts an optional replacer function or array to filter/transform properties, and a space argument (like 2) to pretty-print the output with indentation ā useful for debugging output or human-readable exported files, versus the default single-line compact string.
Frequent Bugs
JSON.stringify() silently drops properties whose value is a function, undefined, or a Symbol.
JSON has no representation for functions, undefined, or Symbols, so JSON.stringify() simply omits object properties with those values (and converts them to null inside arrays) rather than throwing an error. If you need to preserve that data, convert it to a JSON-compatible form first, such as storing a function's behavior as a string key the receiving code interprets separately.
JSON.parse() throws 'Unexpected token' when parsing a value that was never actually stringified as JSON.
This usually means the string being parsed isn't valid JSON ā a common cause is trying to JSON.parse() a plain string that was stored without JSON.stringify() first, or parsing an API's error response (often HTML or plain text) as if it were the expected JSON payload. Always verify the source actually returned JSON before parsing, and wrap the call in try/catch.
Real-World Examples
Round-Tripping an Object Through localStorage with JSON
A settings panel needed to persist a nested preferences object to localStorage (which only stores strings) and reliably reconstruct the same object structure on the next page load.
const prefs = { theme: 'dark', notifications: { email: true, sms: false } };
localStorage.setItem('prefs', JSON.stringify(prefs));
// On next load:
const saved = JSON.parse(localStorage.getItem('prefs') || '{}');
console.log(saved.notifications.email); // true