C.W.K.
Stream
Lesson 02 of 08 · published

type, properties, required — The Three Workhorses

~10 min · json-schema, type, properties

Level 0Plaintext
0 XP0/64 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete

The shape declaration core

Three keywords carry most JSON Schema work: type declares the JSON type, properties declares the keys of an object, required lists which keys must be present.

Type values

The seven type values map to JSON's six plus a split: "string", "number", "integer", "boolean", "null", "object", "array". Note integer is a subtype of number — the validator checks the value has no fractional part.

Type unions

Pass an array to type for unions: "type": ["string", "null"] means 'string or null', a common shape for nullable fields.

required is opt-in

By default, all properties are optional. required is an array of property names that must be present. Omitting required means everything is optional — the schema validates an empty object as valid.

'required' lives next to 'properties', not inside it. A common bug is putting required: true inside each property — that's Draft-04 syntax, removed in Draft-06+. Modern JSON Schema uses an array at the object level: "required": ["name", "email"].

Code

Object with required + optional fields·json
{
  "type": "object",
  "properties": {
    "id":         { "type": "integer" },
    "name":       { "type": "string" },
    "email":      { "type": "string" },
    "created_at": { "type": "string" },
    "deleted_at": { "type": ["string", "null"] }
  },
  "required": ["id", "name", "email"]
}
Nested object types·json
{
  "type": "object",
  "properties": {
    "user": {
      "type": "object",
      "properties": {
        "name": { "type": "string" },
        "email": { "type": "string" }
      },
      "required": ["name", "email"]
    }
  },
  "required": ["user"]
}
Type union (nullable)·json
{
  "type": "object",
  "properties": {
    "middle_name": { "type": ["string", "null"] }
  }
}

External links

Exercise

Take a small JSON document you've worked with recently (an API response, a config). Write a JSON Schema for it from scratch — declare types, list required fields. Run a validator (ajv or Python jsonschema). Now break the document one field at a time and watch the error messages tell you exactly what failed.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.