Request and response body: types, null and empty values
JSON has three ways to say no value: an absent field, null and an empty string, and the contract defines what each means. In JSON Merge Patch an absent field is unchanged while null clears the value, so state is checked with a second request. Money goes as integers in minimal units or a decimal string, never a float, large ids are compared as strings, and bad input gives 4xx, never 500.
API · Request anatomy
A PATCH with null changed nothing. A server bug, or is it by design?
A user wants to remove the middle name from a profile. The frontend sends {"middleName": null}, the server answers 200 and... the field stays. The developer explains that "null and an absent field are the same for us". Who is right depends on the contract, but the confusion itself is typical: JSON has several ways to say "there is no value", and each means something different. Most defects in a request and response body sit exactly at the junction of types and empty values.
Types and three kinds of "empty"
JSON (RFC 8259) has six types: string, number, true/false, null, array, object. For one optional field there are three different requests:
{ "middleName": null } // set to "no value"
{ } // field is not in the body
{ "middleName": "" } // an empty string
The contract has to say what each one means. In a PATCH following JSON Merge Patch (RFC 7396), a field missing from the body is left unchanged, and null removes the value. An empty string is already a value, and it may be invalid (minimum length) or valid. In a POST a missing optional field usually means "use the default", and null is either rejected or treated as "not set".
An absent field versus null in PATCH
The field is not in the body
{ "nickname": "olena" }: middleName is not touched and stays as it was.
The field is null
{ "middleName": null }: the value is cleared, and GET returns null or omits the field afterwards.
Why it is like this. The protocol has no separate word for "leave as is", so partial updates needed an agreement: an absent field means "do not touch", null means "remove". That is why PATCH distinguishes these states, while in PUT (full replacement) an absent field usually means "value reset" or an error.
Example: catching a silently swallowed null
The server answers 200 to a PATCH, and then GET shows the old value:
PATCH /v1/users/7 HTTP/1.1
Content-Type: application/merge-patch+json
{ "middleName": null }
HTTP/1.1 200 OK
Content-Type: application/json
{ "id": 7, "firstName": "Olena", "middleName": "Ivanivna" }
The status is right, the body looks right, but middleName was not cleared. It shows only on a second request after the change, so the test checks the state, not just the response to PATCH:
const MERGE_PATCH = { 'Content-Type': 'application/merge-patch+json' };
test('PATCH null clears the field, absence keeps it', async ({ request }) => {
await request.patch('/v1/users/7', { headers: MERGE_PATCH, data: { middleName: null } });
let user = await (await request.get('/v1/users/7')).json();
expect(user.middleName ?? null).toBeNull();
await request.patch('/v1/users/7', { headers: MERGE_PATCH, data: { nickname: 'olena' } });
user = await (await request.get('/v1/users/7')).json();
expect(user.nickname).toBe('olena');
expect(user.middleName ?? null).toBeNull(); // not touched by the second PATCH
});
The test checks both sides: null clears the field, and a request without the field leaves it alone. The cause of such a defect is often a deserializer that turns null and absence into the same "not provided".
Types, numbers, money, dates
- Type.
"amount": "150"instead of150: the server either rejects it (400/422) or silently coerces it. Both behaviours are possible; it is a bug when they differ between endpoints or the server fails with 500. - Money. Floating-point fractions are unsuitable:
0.1 + 0.2does not equal0.3. The amount is returned as an integer in minor units (cents) or as a decimal string. Check rounding, zero, minimum, maximum, negatives. - Large integers. JavaScript safely holds integers up to 2^53 − 1 (
Number.MAX_SAFE_INTEGER), so large ids are often returned as strings. The test should compare them as strings. - Dates. The standard format is ISO 8601 / RFC 3339 with a time zone,
2026-11-09T10:15:00Z. Check the format in the response, invalid dates (2026-02-30), time zones and day boundaries. - Strings. Minimum, maximum and one over, spaces at the edges, Unicode (Cyrillic, emoji), characters for injections.
- Arrays. Empty, one element, many, duplicates,
nullinside.
A typical table of values is handy to keep in a parameterised test:
const amounts = [
{ value: 0.01, ok: true }, // minimum
{ value: 0, ok: false }, // zero
{ value: -5, ok: false }, // negative
{ value: '150', ok: false }, // a string instead of a number
{ value: null, ok: false }, // null
{ value: 1e21, ok: false }, // above the allowed maximum
];
for (const a of amounts) {
test('amount ' + JSON.stringify(a.value), async ({ request }) => {
const res = await request.post('/v1/accounts/42/transfers', {
data: { toAccount: 'UA21...', amount: a.value, currency: 'UAH' },
});
expect(res.status()).toBe(a.ok ? 201 : 422);
});
}
The expectations here are illustrative: the contract says 422 means rejection and 201 success. In a real project the code comes from the documentation.
Limits and alternatives
- An unknown field is either ignored or rejected with 400. Which one is up to the contract; what matters is that it is the same everywhere. If it is ignored, the value must not be saved (otherwise it is mass assignment).
- An empty body where one is required, and broken JSON, give 400; a body that is too large gives 413. None of these cases should end in 500.
- Schema validation (JSON Schema, OpenAPI) catches types, required fields and formats automatically, but rules like "the amount is not above the balance" are checked separately.
- Not every API distinguishes an absent field from
null. If it does not, that must be written in the documentation, and the test checks exactly that behaviour.
Common mistakes
- Not distinguishing the three "empty" states in test cases. One case "empty field" hides three different behaviours.
- Checking only the response to PATCH, not the state after it. The server can return 200 and change nothing.
- Comparing large ids as numbers in JS. The test passes because the loss of precision distorted both sides equally.
- Accepting 200 for a wrong type. Silently coercing
"abc"to0stores garbage in the database.
Check yourself
A PATCH with {"middleName": null} returns 200, and GET still returns the old value. What do you check and what is probably broken?
Answer
Check the state with a second GET, as in the test above. Most likely the deserializer does not distinguish null from an absent field and treats both as "not provided". If the contract says null clears, it is a bug; if not, the documentation needs clarifying.
Why is "amount": 0.1 + 0.2 dangerous in a money API, and what should be done?
Answer
Floating-point numbers do not store decimal values exactly: the result is 0.30000000000000004. The amount is returned as an integer in minor units or a decimal string, and the test compares the value without loss.
amount field I check the type, the limits (zero, minimum, maximum, negatives), null, the format and precision, plus extra and missing fields. Middle/Senior: I distinguish three states: an absent field, null and an empty string, because in PATCH they mean different things ("do not touch" and "clear"). I check the state after a change, not just the response. Money is kept in minor units, large ids are compared as strings, and invalid input must give 4xx, never 500.