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);- Open the previous project and record its result.
- Type the example and complete: Parse an object, array, broken JSON, and missing-topic data with clear outcomes.
- Change one input, predict, then verify.
- 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
| Symptom | What to check |
|---|---|
| Unexpected result | JSON has no comments, trailing commas, or single quotes; parsing needs recovery. |
| Nothing changes | Save, 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