Home / Blog / JSON tools

JSON tools

JSON.stringify an Array of Objects: Preserve the Intended Values

Serialize an array of records, check the round trip, and handle omitted fields, non-finite numbers and large identifiers deliberately.

Serialize the array once

Call JSON.stringify(records) on the JavaScript array to produce JSON text. The result is a string containing an array, not a new array object. Keep that distinction clear when sending a request, saving a file or passing the result to another function. Stringifying the result again adds a second encoding layer that the receiver must explicitly unwrap.

For a small inventory, preserve the item identifier, quantity and availability using their intended types. The example below produces compact JSON and then reads it back. Comparing selected decoded values establishes that the useful data survived; comparing the visual appearance of escaped text does not answer that question.

const records = [
  { id: "part-7", quantity: 2, available: true },
  { id: "part-8", quantity: 0, available: false }
];
const text = JSON.stringify(records);
const decoded = JSON.parse(text);
console.log(decoded[1].quantity); // 0
console.log(decoded[1].available); // false

Check fields that JSON cannot represent directly

A property whose value is undefined is omitted from an ordinary object serialization. An undefined array element becomes null. Those outcomes have different meanings to an API that distinguishes a missing property from an explicit empty value. Decide which meaning your request contract needs before constructing the array.

A non-finite numeric value also needs attention. An accidental division by zero can become null when serialized, concealing a calculation problem if the receiving service accepts null. Validate numerical results before building the payload rather than treating successful serialization as proof that every value is meaningful.

JSON.stringify([{ note: undefined, score: Infinity }]);
// '[{"score":null}]'
JSON.stringify([undefined]);
// '[null]'

The JSON.stringify reference documents these conversions and the effects of replacers and custom serialization. When an object has its own serialization behavior, review the resulting payload instead of assuming every enumerable-looking application field will appear unchanged.

Choose request fields explicitly

Prefer building a small request object from approved fields when application records also contain internal state. For example, a record may include a temporary selection flag, cached display label and local error message that the server does not need. Mapping each record to the request's intended fields makes that boundary easier to inspect than serializing the whole UI model.

Represent exact large identifiers as strings from the point where they enter the application. Turning an already rounded JavaScript number into a string cannot recover lost digits. BigInt is a separate JavaScript type and ordinary serialization throws for it; convert deliberately to the representation required by the receiving contract, rather than installing a broad coercion that changes every numeric field.

Circular references also cause serialization to fail. A parent object pointing to a child that points back to the parent is an application graph, not a JSON tree. Select the data needed for transport instead of deleting arbitrary references until the error disappears.

Verify a representative round trip

Test an empty array, two distinct records, a zero quantity, a false flag and a string containing a quotation mark or newline. Confirm that array order is retained where the contract depends on order. If the service treats records as a set keyed by identifier, test that interpretation separately; serialization itself does not deduplicate records.

The local Stringify to JSON Online tool offers parse and stringify modes for JSON text. It does not execute arbitrary JavaScript object expressions or callbacks. Use valid JSON as its input and select the intended mode. To investigate JavaScript-only values such as undefined or BigInt, run a controlled example in the runtime used by your application.

Finally, inspect the actual request body or saved file. A correct local variable can still be encoded twice by a request wrapper. A receiver expecting an array should decode an array after one JSON parse, with the expected record count and field types.