Dev Helpers

JavaScript / Data exchange

Encode a Unicode JSON document

Dev Helpers · September 17, 2026

JSON text and transport bytes are separate representations. Encode the text as UTF-8 before measuring or sending a byte payload.

Worked example

const record = { title: "Café", copies: 20 };
const bytes = new TextEncoder().encode(JSON.stringify(record));
console.log(JSON.parse(new TextDecoder().decode(bytes)));

Result

{ title: 'Café', copies: 20 }

Implementation notes

Measure a byte limit after encoding. Counting UTF-16 units can allow a payload to exceed a transport quota even when the string length looks safe.

Try it yourself

Compare string length with byte length for an accented character.

Open the JavaScript example →

Editorial date September 17, 2026 · Published December 6, 2026

Related articles

Sum distances with a Euclidean norm

A diagonal is longer than either of its components. Math.hypot combines perpendicular distances without making the caller square each input.

Build an OS-independent path

Path conventions belong to the destination. Use an explicit path flavour when generating portable asset names.

Resolve ranked ties

A leaderboard needs an explicit tie policy. Ranking tied scores and sorting their display order are separate decisions.