TetSharing
JavaScript tutorial · 19 chapters
  1. Get started
  2. let, const and var
  3. Data types
  4. Operators and equality
  5. Strings
  6. Template literals
  7. if, else and switch
  8. Loops
  9. Functions
  10. Arrow functions
  11. Arrays
  12. map, filter, reduce
  13. Objects
  14. Destructuring and spread
  15. Classes
  16. Errors and try...catch
  17. Promises
  18. async and await
  19. JSON

Final JavaScript quiz
Cheat sheet: javascript array methods

Chapter 19 of 19 · By Ethan Cole · Updated November 21, 2026

JavaScript JSON: JSON.stringify and JSON.parse

JSON (JavaScript Object Notation) is a text format for data. It borrows JavaScript's syntax for objects and arrays, but it is just text, so any language can read and write it. Web APIs, configuration files and browser storage all use JSON. JavaScript converts with two functions: JSON.stringify() turns a value into JSON text, and JSON.parse() turns JSON text back into a value.

What JSON allows

JSON is stricter than JavaScript:

  • Keys must be strings in double quotes.
  • Strings use double quotes only.
  • Values can be strings, numbers, true, false, null, arrays and objects. There is no undefined, no functions, no dates, no comments, and no trailing commas.

JSON.stringify

JSON.stringify(order) produces a compact single line. Properties whose value is undefined or a function are left out entirely, which is why note disappears and "note" in back is false after the round trip. A Date becomes an ISO 8601 string, such as "2026-11-21T00:00:00.000Z", because dates define a toJSON() method. When parsed back, it stays a string; convert it with new Date(text) if you need a date again.

The third argument controls indentation for human-readable output: JSON.stringify(value, null, 2) indents nested levels by two spaces. The second argument, here null, can filter or transform properties.

JavaScript · editable

Expected output

{"id":7,"customer":"Liam","items":["River of Salt","Paper Cranes"],"paid":true}
string
Paper Cranes true false
{
  "a": 1,
  "b": [
    1,
    2
  ]
}
{"when":"2026-11-21T00:00:00.000Z"}
SyntaxError

JSON.parse

JSON.parse(text) returns ordinary arrays and objects that you use like any other value. If the text is not valid JSON, for example because it uses single quotes or unquoted keys like {name: 'Ava'}, it throws a SyntaxError. Data from outside your program can be malformed, so wrap JSON.parse in try...catch when the input is not under your control.

JSON in practice

With fetch(), call await response.json(), which reads the body and parses it in one step. To save data in the browser, store JSON.stringify(value) in localStorage and parse it when reading it back.

A common trick for copying plain data is JSON.parse(JSON.stringify(obj)). It works, but drops undefined, turns dates into strings and fails on circular references. structuredClone() is the better tool.

Common mistakes

  • Writing JSON by hand with single quotes or trailing commas.
  • Expecting dates to come back as Date objects.
  • Parsing data twice, or calling JSON.parse on something that is already an object.

Summary

  • JSON.stringify converts values to JSON text; JSON.parse converts back.
  • JSON needs double-quoted keys and strings and has no undefined, functions or comments.
  • Pretty-print with JSON.stringify(value, null, 2).
  • Invalid JSON makes JSON.parse throw a SyntaxError.

Quiz: JSON

1. What is JSON.stringify({ a: undefined, b: 1 })?

Properties with undefined values are omitted.

2. Which string is valid JSON?

JSON requires double-quoted keys and strings and forbids trailing commas.

3. What does JSON.parse do with invalid input?

Malformed JSON causes a SyntaxError.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.