CCA Prep

Lesson 1.6Lesson 6 of 24

The JSON object

Given a JSON response, demonstrate how to operate the JSON object.

The JSON objectJavaScript valueobjects, arrays, strings, numbersJSON.stringifytoJSON, replacer, indentJSON textno undefined, functions, Map or SetJSON.parsereviver; throws on bad textNew plain datadates come back as strings

Key points

  1. 1JSON.parse turns JSON text into values and throws a SyntaxError for invalid text, so wrap untrusted input in try/catch.
  2. 2JSON.stringify takes a replacer, a function or an allow list of keys, and a number of spaces to indent.
  3. 3A reviver passed to JSON.parse sees every key and value, and can turn strings back into numbers or dates.
  4. 4Undefined values and functions are left out of objects and become null in arrays. NaN and Infinity become null.
  5. 5Dates become ISO strings and stay strings after parsing. Map and Set become empty objects.
What the exam guide checks here

Objective wording from the exam guide (English).

  • 1.6.K1JSON.parse and JSON.stringify, including reviver, replacer and indentation
  • 1.6.K2What JSON cannot represent: functions, undefined, Date objects, Map, Set
  • 1.6.S1Read, change and serialize data from a JSON response

Lock it in

Answer scenario questions on this topic. Every option comes with an explanation.

Practise 1.6 · 12 questions