JSON & API-shaped data
Understand the idea
A request succeeds but the screen still crashes: the response contains where the UI expected an . Valid JSON is not the same thing as valid application data.
A delivery format, not executable code
A server cannot send a live JavaScript directly through a text response. JSON gives both sides an agreed format for numbers, text, booleans, , objects, and . Your program parses that text, then checks whether the result has the fields the task needs.
What JSON can contain
JSON.stringify turns supported values into text. JSON.parse reads JSON text and returns the represented value, which might be an object, array, string, number, boolean, or null. JSON requires double-quoted names and does not allow comments or trailing commas.
JSON is stricter than a JavaScript object literal. Property names and string values use double quotes. Comments, trailing commas, , and undefined are not JSON values. A JSON document can also be a single string or number; successful parsing does not an object at the top level.
| Value or operation | JSON behavior | Consequence |
|---|---|---|
| JSON.parse(text) | Returns a JavaScript value or throws | Catch malformed text, then validate the result |
| JSON.stringify(value) | Produces JSON text when representable | It is serialization, not a general-purpose deep clone |
| Object property with undefined | Omitted during stringify | The field can disappear in transit |
| Array entry with undefined | Becomes null during stringify | Array positions are preserved differently |
| BigInt or circular references | Ordinary stringify throws | Choose an explicit serialization strategy |
| Date object | Usually serialized as a string | Parsing does not restore a Date instance |
Parsing and serializing
JavaScript objects support more than JSON can express. Functions and undefined object properties are omitted during serialization; unsupported array entries become null. Dates normally serialize to strings, bigint throws without a custom strategy, and circular references cannot be serialized directly. Do not assume a stringify/parse round trip is a general-purpose deep clone.
Inspect the delivered shape
After parsing, check the shape at the boundary. An can validly return an error object or null instead of the list you expected. Array.isArray, property checks, and field-type checks make these assumptions explicit. Optional chaining can avoid a crash, but it does not prove that a response satisfies the requirements of your screen.
Missing data needs a deliberate meaning
Use defaults only when they mean something. An empty list can be a sensible display fallback, while an absent account ID may require rejecting the response. Catch malformed JSON separately from these shape decisions so the user sees a useful failure instead of a half-rendered result.
The example checks that items is an array before using map. In a real response, you should also check each item's shape before relying on title. Decide whether missing items means an empty list or an invalid response; silently choosing an empty list is appropriate only when that matches your contract.
Mental model
FOLLOW THE VALUE
Parsing crosses the text boundary.
01 / 03Trace the transformation
Live example
The input text is valid . After parsing, the check makes the collection assumption visible before mapping its records.
Your console output appears here.
Guided challenge
Open the JSON delivery
Parse responseText before inspecting its items property.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
Read string
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 150 XP. You can always revisit it.