JSON serialization: prevent API data from being interpreted differently
JSON serialization converts structured values into JSON text for transfer or storage. Cross-language clients need more than valid RFC 8259 syntax: escaping, numeric precision, duplicate object names, and UTF-8 affect whether the same data has the same meaning. JSON Schema checks data structure; it does not guarantee correct serialization or transport encoding.
JSON serialization converts structured values into JSON text for transfer or storage. Cross-language clients need more than valid RFC 8259 syntax: escaping, numeric precision, duplicate object names, and UTF-8 affect whether the same data has the same meaning. JSON Schema checks data structure; it does not guarantee correct serialization or transport encoding.
JSON values and object names
JSON values are objects, arrays, numbers, strings, or the lowercase literals true, false, and null. Object members are string-name/value pairs; arrays are ordered. Do not depend on object-member order, and generate unique names. Parsers differ on duplicate names: they may keep the last value, reject the document, or expose all values.
Strings and safe parsing
Escape quotation marks, backslashes, and control characters U+0000–U+001F inside strings. Represent line breaks as escapes such as \n. Other Unicode characters may be sent directly or escaped. Send JSON text as UTF-8 without a leading BOM. Use the language's JSON serializer instead of manually concatenating quotes around user input. Parse with a JSON parser, never an execution function such as eval; JSON syntax is not identical to JavaScript syntax.
Numbers and interoperability
JSON permits decimal and exponent notation, but implementations differ in precision and range. RFC 8259 identifies -(2^53)+1 through (2^53)-1 as the integer range that interoperates exactly across common IEEE 754 double-precision implementations. If large integer IDs or exact decimal amounts exceed that range, use a representation defined by the API contract, such as strings or scaled integers, and verify lossless round trips in every supported language.
Keep object names unique. Duplicate names can produce different interpretations. Prevent duplicates when generating JSON and check parser behavior at security boundaries. Do not assume keys are equivalent after case folding or Unicode normalization; define exact names in the API contract.
Serialization is not validation
A serializer creates syntactically valid JSON text; a parser reads it back. Schema validation checks whether the parsed value meets application structure requirements. Valid JSON can fail the API schema, and schema-valid data can still violate permissions or business rules.
Before release, verify real parsers reject invalid syntax; UTF-8 and escaping; duplicate-name and numeric policies; schema, authorization, and business-rule checks as separate stages; and lossless serialization/parsing of the same sample values in every supported client language. JSON syntax alone does not guarantee compatibility. Document API-specific size, nesting, precision, field semantics, and parser resource limits.
Sources
[RFC 8259: JSON Data Interchange Format](https://www.rfc-editor.org/rfc/rfc8259.html)
[JSON Schema Draft 2020-12](https://json-schema.org/draft/2020-12)