null, missing or empty? Testing optional fields in JSON APIs
A mobile app crashes on a user without a middle name. The API used to omit the field; after a refactor it returns "middleName": null. Both are valid JSON, both "mean" no middle name, and the client handled only one of them.
Optional fields have three states, and an API should be deliberate about each.
Responses: present-as-null or omitted
Both conventions are fine. Mixing them is not. Write the rule down and test it:
// Rule: optional fields are always present, null when unset
const user = pm.response.json();
pm.test('middleName is always present', () => {
pm.expect(user).to.have.property('middleName');
});
pm.test('middleName is a string or null, never empty', () => {
if (user.middleName !== null) {
pm.expect(user.middleName).to.be.a('string');
pm.expect(user.middleName).to.not.equal('');
}
});Or without code, a JSON Schema assertion on the request's Tests tab (source JSON body, path $, operator matches JSON schema):
{
"type": "object",
"required": ["id", "firstName", "middleName", "lastName"],
"properties": {
"middleName": { "type": ["string", "null"], "minLength": 1 }
}
}required makes "missing" a failure; "type": ["string", "null"] allows null; minLength: 1 forbids the empty string.
Lists: empty array, not null
For collections, almost every client prefers [] over null. A null list makes every client write (user.tags || []).map(…) and the one that forgets crashes:
pm.test('tags is always an array', () => {
pm.expect(pm.response.json().tags).to.be.an('array');
});PATCH: the case that matters most
For partial updates, the three states usually mean:
| Body | Meaning |
|---|---|
{} (field missing) | leave middleName as it is |
{"middleName": null} | clear middleName |
{"middleName": ""} | either clear it, or reject it with 400: decide |
Test each one against a user who has a middle name:
PATCH {{baseUrl}}/users/{{userId}}
Content-Type: application/json
{"firstName": "Ann"}// After "PATCH without middleName", read the user back
pm.test('missing field leaves the value alone', () => {
pm.expect(pm.response.json().middleName).to.equal('Maria');
});PATCH {{baseUrl}}/users/{{userId}}
Content-Type: application/json
{"middleName": null}// After "PATCH with null", read back
pm.test('null clears the value', () => {
pm.expect(pm.response.json().middleName).to.equal(null);
});The classic bug is the first one: a PATCH handler that maps the body onto a struct, where a missing field becomes the zero value and overwrites the stored one.
Booleans and numbers
The same three states apply to false and 0, with a twist: code that checks "is set" with a truthiness test treats false and 0 as unset.
PATCH {{baseUrl}}/users/{{userId}}
Content-Type: application/json
{"marketingEmails": false, "creditLimit": 0}Read it back and check that both were stored. If false didn't stick, someone wrote if (body.marketingEmails).
Requests: what does the API accept?
Send null for a required field and an empty string for a required field. Both should be 400 with the field named, not a 500 from a database NOT NULL constraint. Negative testing covers more of this.