Structured output

Duplicate JSON keys and large integers: two failures a schema can miss

See how duplicate fields and large numeric identifiers can change before schema validation, and build small regression fixtures.

By AI Output Check editorial · Published

Original worked examples are hypothetical, not measured product results. Sources checked 2026-11-09.

Validation starts before the schema

A schema validates the value a parser gives it. If parsing has already discarded a repeated field or rounded a number, a later type check cannot recover the original text. This matters for structured AI output, imported records and any service that accepts JSON from another system.

Output Contract Inspector checks more than JSON syntax. Before contract validation, its lexical checks reject duplicate object keys and selected numeric-loss cases. That is an explicit local policy, not a claim that every JSON parser behaves the same way. Understanding the boundary helps you reproduce failures in the application that will eventually consume the response.

A duplicate field is not a harmless extra field

Take this fictional payload: {"status":"pending","status":"approved"}. A consumer that keeps the last occurrence sees approved. Another consumer might reject the input. A person scanning the text may notice pending first. Even a schema restricting status to those two strings cannot determine which occurrence was intended after one has been discarded.

The JSON interoperability standard recommends unique names within an object and describes differing behavior when names repeat. Treat duplicate names as an input defect rather than choosing whichever value looks more convenient. Return to the producer or a trusted source and ask for one unambiguous field.

Escaping adds another case worth testing. In {"id":"A","\u0069d":"B"}, the two written names decode to the same property name. The inspector rejects escaped equivalents too. A simple text search for an identical sequence of characters would not be enough.

Technical references: RFC 8259: JSON interoperability

Keep identifiers out of numeric arithmetic

Now consider a fictional external record identifier written as the JSON number 9007199254740993. JavaScript’s ordinary numeric representation cannot preserve every integer at that magnitude. Once parsed into a rounded value, checking that it is an integer does not prove it is the identifier that arrived.

If an identifier is a label rather than a quantity, represent it as a string: {"record_id":"9007199254740993"}. That keeps its characters intact through ordinary JSON parsing. A string is also a better fit for identifiers with leading zeros, such as "000412", where numeric conversion would discard meaningful formatting.

Changing a field from number to string is a contract change, not a cosmetic repair. Coordinate it with producers, database mappings and consumers. Do not silently rewrite production records because an example works in a local inspector.

Technical references: RFC 8259: JSON interoperability

Exact decimal amounts need their own design

A number that avoids the unsafe-integer boundary is not automatically an exact financial decimal. The inspector still uses JavaScript floating-point numbers for ordinary decimals. It is not an exact-decimal arithmetic engine.

For a fictional price of 12.34, an application might store an integer number of minor units, 1234, together with an explicit currency, if that matches its domain. Another application might use a validated decimal string and a decimal arithmetic library. Either choice requires rules for scale, rounding and currency; merely changing a schema type is not the whole solution.

The inspector also rejects nonfinite parse results and numbers that underflow to zero. These guards catch specific lossy cases. They do not replace reviewing the numeric range and precision requirements of your actual application.

Build a parsing regression set

Run the following cases through both the inspector and your real ingestion path. Record the exact received text, parser outcome and business decision without placing sensitive production payloads in logs.

  • Two identical status keys with conflicting values: reject rather than silently choose.
  • An id key and its escaped equivalent: detect the decoded duplicate.
  • A large identifier represented as a number: reject or handle through an explicitly designed lossless parser.
  • The same identifier as a string: preserve every character.
  • An identifier with leading zeros: preserve the zeros if they are meaningful.
  • An ordinary decimal: test the arithmetic and rounding policy separately from schema validation.

Separate parsing, structure and meaning

Once parsing is trustworthy, validate required fields, types and allowed values. Then check business meaning: whether a record exists, whether a status transition is allowed, and whether an amount reconciles. These stages answer different questions.

Use synthetic fixtures for the first investigation. If a real response fails, retain only the evidence needed under your own data-handling rules. The inspector’s diagnostic export can contain property names and parser excerpts, so review the report before sharing it. A successful local check means the supplied representation passed the supported rules, not that the record is true or safe to act on.

Sources and scope

Provider rules and supported features can change. These sources support the technical background; the worked examples and decision checklists are authored for this article.

Read next

All articles →