CheckMySchema

Structured data explained

Is Null Valid JSON? Test the Difference Between Null and a Missing Key

Try explicit null, an omitted property and an empty string against the same tested JSON Schema. Check the exact failing keyword.

    Three blank cards and a closed laptop rest on a desk.
    Save one clearly identified fixture per serialized state.
    CheckMySchemaCheckMySchema

    At a glance

    JSON permits null. In an object, a null value and an absent key behave differently under separate type and presence rules.

    Validate a representative data fixture

    JSON supports null as its own value. In an object such as {"caption":null}, the caption key exists. In {}, it does not. A JSON Schema can require the key and still allow its value to be null. Test the key and the value separately so you can tell which rule produced an error.

    Give the example field two independent rules

    Imagine a card that can have a written caption or an explicit empty state. In Draft 2020-12, set the object type and give the caption its own schema in properties. For the caption, use {"type":["string","null"],"minLength":1}. The type accepts either a string or null. The length rule checks strings, so it rejects an empty string and leaves a null value alone.

    Now give the object the separate rule "required":["caption"]. That rule asks whether the key appears. It never checks the caption's length or substitutes a default for the missing key.

    One box checks whether there is a caption key. A separate box checks the type and length of the value.
    Write assertions for the two rules separately.

    Check the applicable validation keywords in Draft 2020-12 validation vocabulary.

    Sources: Draft 2020-12 validation vocabulary · Understanding JSON Schema: objects · Understanding JSON Schema: null

    Run three small examples before changing the rule

    Try {"caption":null}, {} and {"caption":""}. In the local Ajv Draft 2020-12 fixture, the explicit null passes, the empty object fails required and the empty string fails minLength. A numeric caption fails type. A nonempty string, including the word "null" in quotes, passes the string rule.

    Test the serialized input as well. A value that appears blank in an editor could be an empty string, a string of spaces or no key at all. The schema above accepts a space as a one-character string. If the real contract excludes whitespace-only captions, add and test a separate rule for that case.

    Tested with Ajv 8.20.0 and Draft 2020-12.
    Serialized exampleRequired keyOptional key
    {"caption":null}PassPass
    {}Fail: requiredPass
    {"caption":""}Fail: minLengthFail: minLength
    {"caption":7}Fail: typeFail: type
    The omitted key fails required, explicit null passes, and empty text fails minLength.
    Keep the exact JSON state and failing keyword together.

    Sources: JSON Schema · IETF

    Change presence only if the contract allows it

    If the caption may be absent, remove the caption from required and keep the same rule under properties. The empty object now passes. The explicit null still passes, and the empty string still fails the same length check. Conversely, if a present caption must be a string, remove the null option from its type while keeping whatever presence rule the contract needs.

    Keep the example response or file that demonstrates what the producer really sends. If different producers use the states differently, document that behavior before you loosen the schema.

    Removing the requirement accepts absent keys. Removing the null type causes a present null to fail.
    Re-run the expected examples for the one change you intended.

    Which part of the contract changed?

    Allow an omitted caption

    Remove caption from the required array. Keep the schema for a caption that is present.

    Allow an explicit null

    Include null in the caption type. Keep its presence requirement if the key must appear.

    Read the required-property examples in Understanding JSON Schema: objects.

    Sources: Understanding JSON Schema: objects · Draft 2020-12 validation vocabulary

    Save test names that describe the evidence

    Name the fixtures by what was serialized. Useful names include “caption absent,” “caption present as null,” and “caption present as empty text.” Keep the failing keyword beside the expected outcome. If a later change converts one state into another, you can see which contract it changed before a broader content validation run.

    Two plain cards and a pencil sit against a gray desk.
    Label the failing rule alongside its example input.

    Keep three kinds of values distinct

    • Read the actual serialized value.
    • Check the key’s presence rule.
    • Check the quoted or unquoted value type.
    • Keep the expected error keyword in the test.

    For the related task, see Review empty and missing values.

    Sources: Understanding JSON Schema: null · Draft 2020-12 validation vocabulary

    Frequently asked questions

    Is "null" in quotes the same as null?

    A quoted value is a string. The bare literal null is a separate JSON value.

    Does minLength reject null?

    The minLength keyword applies to strings. The type rule determines whether null is allowed.

    Does required replace an absent key with a value?

    It checks whether the property exists. It does not populate or substitute a value.

    Is null at the top level a valid JSON document?

    JSON permits a value at the top level. If a schema requires an object, that schema will reject a top-level null.

    CheckMySchemaCheckMySchema

    Check a JSON Schema with a validator

    Run representative data against the intended schema draft.

    Validate a representative data fixture

    Continue with a related guide

    References