JSON guide · 2 min read
JSON Schema: Required, Optional and Nullable Fields
Model field presence and nullability correctly in JSON Schema and avoid treating missing, null and empty values as the same state.
required controls presence, not value type
In an object schema, required lists property names that must exist. It does not automatically say whether a present property's value may be null.
{
"type": "object",
"required": ["name"],
"properties": {
"name": {"type": "string"}
}
}An optional property can still reject null
If a property is omitted from required, it may be absent. When it is present, it must still satisfy its declared schema. A property whose only allowed type is string therefore rejects null even when optional.
Allow null explicitly when the contract permits it
Modern JSON Schema can express a union of allowed types. If a field can be either a string or null, declare both instead of relying on an application-specific assumption.
{
"properties": {
"middleName": {"type": ["string", "null"]}
}
}Required and nullable can be combined
A property can be required to exist while allowing null as its value. That differs from an optional non-nullable property, which may be absent but must have a valid non-null value when present.
Empty values need their own constraints
An empty string is still a string, an empty array is still an array and an empty object is still an object. If those empty values are invalid, use constraints such as minLength, minItems or minProperties instead of treating them as null.
Test the complete state matrix
For important properties, write examples covering missing, present-valid, present-null, present-empty and wrong-type cases. This makes the intended contract obvious and prevents later changes from collapsing distinct states.
- Property missing.
- Property present with valid value.
- Property present with null.
- Property present with an empty value.
- Property present with the wrong type.
Common questions
Frequently asked questions
Does required mean a field cannot be null?
No. required controls whether the property exists. Nullability is controlled separately by the property's schema.
Can an optional string property be null?
Not unless the schema explicitly allows null. Optional means it may be absent, not that every value is allowed when present.
How do I reject empty strings?
Use a string constraint such as minLength: 1 in addition to the presence and type rules appropriate for the contract.