Read JSON and validate its shape

Convert between text and JavaScript values and validate fields before use.

Difficulty
beginner
Time
40 minutes
Last verified
9/21/2026

What you will build

Convert between text and JavaScript values and validate fields before use.

Understand the idea first

Convert between text and JavaScript values and validate fields before use.

Build it step by step

const raw = '{"topic":"HTML","minutes":35}';
const value = JSON.parse(raw);
if (typeof value.topic !== 'string') throw new Error('Invalid topic');
const output = JSON.stringify(value, null, 2);
  1. Open the previous project and record its result.
  2. Type the example and complete: Parse an object, array, broken JSON, and missing-topic data with clear outcomes.
  3. Change one input, predict, then verify.
  4. Create and repair: JSON has no comments, trailing commas, or single quotes; parsing needs recovery.

Read the important lines

  • Convert between text and JavaScript values and validate fields before use.
  • Read input, processing, and output separately.
  • Verify another input and an edge case.

Common mistakes and what to check

SymptomWhat to check
Unexpected resultJSON has no comments, trailing commas, or single quotes; parsing needs recovery.
Nothing changesSave, verify paths, and read the first relevant Console error.

Practice without copying

Parse an object, array, broken JSON, and missing-topic data with clear outcomes.

Check the answer after you try

Rebuild and explain the decisions. Check: JSON has no comments, trailing commas, or single quotes; parsing needs recovery.

Completion check

  • I produced the required visible result
  • I can explain the input, processing, and output
  • I tested normal, edge, and failure cases

Your privacy choices

Necessary storage is always on. Optional analytics and support services load only after consent.