CheckMySchema

Structured data explained

JSON Object vs. Array and How to Validate the Shape the Consumer Expects

Try one object and a list containing that object against two JSON Schemas. Separate a root type mismatch from a problem with a list item.

    One dark card sits beside a stack of pale cards on a desk.
    Check whether the receiving contract takes one record or an ordered list.
    CheckMySchemaCheckMySchema

    At a glance

    An object contains named properties. An array contains ordered values. A valid JSON object and a valid array can still fail a contract that expects the other shape.

    Review nested objects before validating the contract

    An object is a collection of named properties. An array is an ordered list of values. A document can use either as its root. If an importer expects one object and you send an array with one object inside, the JSON can parse successfully while the shape still fails validation.

    Confirm the expected root shape

    Start with the receiving contract. If it expects one record, use a schema with an object root and named properties. For this example, give the object one required property called id with an integer value. A consumer expecting multiple records can use an array root and an items schema that checks each object the same way.

    Look at the parsed value rather than at the amount of data. A one-item list is still a list. A large single record can have several properties and still be one object. Set the contract so it describes what the consumer expects to take in, including whether a list may be empty.

    A single JSON object on the left contrasts with an array containing that object on the right.
    Check which value type the receiving contract expects.

    Two shapes need two contracts

    One named record

    Check the root type and the properties on that object.

    A list of records

    Check the root array, then validate the members with an item schema.

    For the JSON structure behind the comparison, see RFC 8259, JSON objects and arrays.

    Sources: Understanding JSON Schema, objects · Understanding JSON Schema, arrays · IETF

    Test the object and list against both contracts

    Try a record with id seven and then a list containing the same record. With the single-object schema, the first example passes and the list fails at the root. With the list schema, the first example fails at the root and the list passes because its member meets the same object rule.

    Now submit a list that contains one empty object. It passes the root type check. The first member fails because the required id is missing. That error points to a different location. If you only look at the fact that validation failed, you might change the whole shape when only one member needs attention.

    Local Draft 2020-12 validation of three small synthetic examples.
    JSON valueSingle-record schemaList-of-records schema
    {"id":7}PassRoot type error
    [{"id":7}]Root type errorPass
    [{}]Root type errorMissing id in the first item
    Three rows separate the object schema, the array and its item schema, and an optional minimum length.
    Cardinality is separate from the type of each member.

    For array type and item schema behavior, see Understanding JSON Schema, arrays.

    Sources: Understanding JSON Schema, objects · Understanding JSON Schema, arrays

    Separate root and member errors

    Read the error location before changing the consumer or the data. A root type failure means the first container differs from the contract. An error within item zero means the actual array was reached and the first member should be checked. Look at the required-properties rule and the actual content of that member independently.

    Keep a small fixture without real account or customer data. Confirm the one-object and one-item-list cases and add an invalid member case. If you need at least one record, test an empty array as its own boundary. A correct root shape and matching rules for the contents are separate parts of the contract.

    Two cards compare a wrong top-level type and a missing id at path /0.
    An error inside the first member can mean the root array type already passed.
    A single card and a small stack are arranged in separate groups.
    The same content can appear at the root or inside the first array member.

    Read the error path before changing your JSON

    • Confirm the contract’s root type.
    • Inspect the parsed payload’s root.
    • Check any member errors after resolving the root.
    • Use a small anonymous fixture to test the change.

    For required properties inside an object, see Understanding JSON Schema, objects.

    For the related task, see Keep null and missing fields separate.

    Sources: Understanding JSON Schema, objects · Understanding JSON Schema, arrays

    Frequently asked questions

    Is a one-item array the same as one object?

    No. The array wraps a value and the position is part of the structure.

    Can arrays contain objects?

    Yes. A schema can require every member to satisfy an object schema.

    Does items require at least one array element?

    No. If the consumer needs a non-empty list, also use an appropriate minItems requirement.

    Why does the instance path point to the first item?

    In a validator such as Ajv, a path of /0 can identify a problem in the first member while the root array itself is valid.

    CheckMySchemaCheckMySchema

    Review object nesting

    Check where each object sits within a bigger JSON value.

    Review nested objects before validating the contract

    Continue with a related guide

    References