Understanding null in JSON: When and How to Use It

Understanding null in JSON: When and How to Use It

JSON has a special null value representing "no value":

{
    "name": "Alice",
    "middleName": null,
    "email": "alice@example.com"
}

middleName exists in the object but has no value.

null vs Undefined

JSON doesn't have undefined. Only null exists:

// Valid JSON
{ "value": null }

// NOT valid JSON - undefined doesn't exist
{ "value": undefined }

In JavaScript, JSON.stringify() handles them differently:

JSON.stringify({ a: null })       // '{"a":null}'
JSON.stringify({ a: undefined })  // '{}'  (property omitted)

null vs Empty String

null and empty string mean different things:

{
    "name": "",         // Name is known but empty
    "middleName": null  // Middle name is unknown or not applicable
}

Empty string is a value. null is the absence of value.

null vs Omitting the Field

Two ways to represent "no value":

// With null
{ "name": "Alice", "phone": null }

// Field omitted
{ "name": "Alice" }

Use null when the field's absence is meaningful. Omit when the field is truly optional.

When to Use null

Database NULL mapping:

// SQL column can be NULL
SELECT name, phone FROM users;
// Returns: Alice, NULL

// JSON representation
{ "name": "Alice", "phone": null }

Optional reference that might not exist:

{
    "userId": 123,
    "managerId": null  // User has no manager
}

Resetting a value:

// PATCH /users/123
{ "bio": null }  // Clear the bio field

When NOT to Use null

For empty collections:

// Bad
{ "items": null }

// Good
{ "items": [] }

For missing numbers (when 0 has meaning):

// Ambiguous
{ "count": null }

// Better - make it optional
// Either: { "count": 0 }
// Or omit: {}

Checking for null

In JavaScript:

const data = { "value": null };

if (data.value === null) {
    console.log("Value is null");
}

// Checking for null or undefined
if (data.value == null) {
    console.log("Value is null or undefined");
}

Type Systems

TypeScript distinguishes null:

interface User {
    name: string;
    phone: string | null;  // Explicitly nullable
}

API Design Patterns

Partial updates:

// PATCH /users/123
{
    "name": "Alice",     // Update name
    "phone": null        // Clear phone
    // email not included - don't touch
}

Nullable fields in responses:

{
    "user": {
        "id": 123,
        "name": "Alice",
        "avatar": null,     // No avatar uploaded
        "lastLogin": null   // Never logged in
    }
}

Default Values

Handling null with defaults:

const phone = user.phone ?? "No phone provided";
const bio = user.bio || "No bio available";

Database Mapping

Most databases map JSON null to SQL NULL:

// JSON
{ "middleName": null }

// SQL
INSERT INTO users (middle_name) VALUES (NULL);

Further Reading

The JSON specification defines null as a valid value type.

MDN's null documentation explains JavaScript's null handling.

null represents intentional absence in JSON.

0 comments

Leave a comment

Please note, comments need to be approved before they are published.