Skip to content
engineering workbench.by StudyPort Academy
Learning path
Intermediate20 minLESSON 19 / 34

JSON & API-shaped data

01

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.

What survives a JSON round trip?
Value or operationJSON behaviorConsequence
JSON.parse(text)Returns a JavaScript value or throwsCatch malformed text, then validate the result
JSON.stringify(value)Produces JSON text when representableIt is serialization, not a general-purpose deep clone
Object property with undefinedOmitted during stringifyThe field can disappear in transit
Array entry with undefinedBecomes null during stringifyArray positions are preserved differently
BigInt or circular referencesOrdinary stringify throwsChoose an explicit serialization strategy
Date objectUsually serialized as a stringParsing 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.

02

Mental model

FOLLOW THE VALUE

response text
JSON.parse
JavaScript value

Parsing crosses the text boundary.

01 / 03Trace the transformation

03

Live example

The input text is valid . After parsing, the check makes the collection assumption visible before mapping its records.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Open the JSON delivery

Parse responseText before inspecting its items property.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

Read
string

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01Can valid JSON represent null?
    02Does JSON.parse validate your API’s field requirements?

    Ready to call this one understood?

    Mark this lesson complete to earn 150 XP. You can always revisit it.

    Sign in to save progress
    Next lesson