
An API request reaches the server, but a field is missing. Before investigating transport failures, inspect the exact JSON string. JSON.stringify does not preserve every JavaScript value. An object property whose value is undefined is normally omitted, while an undefined array element becomes null. Those outcomes can mean different things to the receiver.
Inspect the payload, not only the object
This small example uses ordinary objects and arrays, without a replacer function or custom toJSON method:
const draft = {
name: 'Sample',
note: undefined,
score: NaN
};
const body = JSON.stringify(draft);
console.log(body);
// {"name":"Sample","score":null}
console.log(JSON.stringify([undefined, NaN, null]));
// [null,null,null]
console.log(JSON.stringify(undefined));
// undefined, not a JSON string
The note property disappears. The non-finite number NaN becomes null. In the array, all three positions survive, but the output no longer tells you which one started as undefined, NaN or null. Parsing that output cannot recover distinctions the text no longer contains.
Define missing, cleared and invalid separately
For a settings update, an absent property might mean 'leave unchanged', while null might mean 'clear this field'. That is an example of an API contract, not a universal JSON rule. Check the receiver's documented behavior before replacing undefined with null everywhere.
I would build a small payload object containing only the fields the endpoint accepts, then validate each one. For an optional note, choose intentionally whether an unfilled form omits the field, sends an empty string or sends null. For a measurement, reject an invalid number or represent its status explicitly rather than letting NaN quietly become a plausible missing value.
const score = 7;
if (!Number.isFinite(score)) {
throw new TypeError('score must be finite');
}
const body = JSON.stringify({ name: 'Sample', score });
This is a narrow numeric check, not a complete schema validator. It deliberately rejects numeric strings. Other fields still need their own types, allowed values and limits.
Some values fail loudly instead
With default serialization, encountering a BigInt throws a TypeError. A circular object reference also throws. Do not assume a successful stringify in one test means every production value will serialize.
JSON.stringify({ id: 12n }); // TypeError
const circular = {};
circular.self = circular;
JSON.stringify(circular); // TypeError
If an identifier must travel as a decimal string, convert it deliberately and make sure the receiver expects that representation. Converting a large BigInt to a Number just to avoid an exception can lose precision. Removing circular references also changes the data; it is not a lossless fix.
Do not use a round trip as a fidelity test
JSON.parse(JSON.stringify(value)) may produce a new object, but it is not a general-purpose copy that preserves JavaScript types and values. The omission and null conversions above already disprove that assumption. Serialization is a boundary with a contract, not a promise to clone arbitrary program state.
What I checked locally
I ran these cases in Node.js: omitted object properties, array null conversion, a top-level undefined result, BigInt and circular-reference errors, and the finite-number guard. No network request was sent. Custom serialization hooks can change results, so test the actual payload-building path as well as an isolated sample.
Use synthetic data when logging a request body. The debugging goal is to see its structure and value policy, not to copy private tokens or customer records into logs.
Comments
Post a Comment