Skip to content
ToolsOnDuty - free online tools

Search tools

Loading tool search...

Development

JSON Schema Inference vs Validation: Review the Contract

See why an inferred schema can reject its own sample, review required fields and test the limits of local JSON validation.

By ToolsOnDuty Editorial Team

JSON Schema Generator inferring integer array items from a sample that also contains a string

Generating a schema from one JSON sample is a quick way to start documentation, but inference is not a complete contract. The most useful review question is whether the resulting rules describe data you actually intend to accept.

This guide follows an actual mixed-array example through the browser generator and validator. It explains both the observed failure and the limits of treating local validation as production assurance.

Separate three different questions

JSON formatting asks whether text can be parsed as JSON and how to present it. Schema inference asks what types and structure appear in a sample. Validation asks whether a value satisfies selected rules. A successful answer to one question does not establish the others.

Use the JSON Formatter for syntax problems, the JSON Schema Generator for a starting structure, and a validator appropriate to the rules your application actually relies on. The local schema validator supports a subset; it is not a complete standards or business-contract certification.

Reproduce a surprising mixed-array result

Enter [1,"two"] in Representative JSON. The schema updates live without a Run button. It declares the draft 2020-12 vocabulary and produces an array whose items have type integer. The second value is a string, but it is not merged into the inferred item rule.

The implementation infers items from the first array element only. An empty array has no inferred item schema. A draft label describes the emitted schema's vocabulary; it does not mean that every sample value was examined or that all rules needed for your application were discovered.

JSON Schema Generator inferring integer array items from a sample that also contains a string
Actual result for [1,"two"]. Only the first array item determines the inferred items rule.
json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "array",
  "items": {
    "type": "integer"
  }
}

Validate the sample against the generated draft

Open the JSON Schema Validator, put the same array in JSON data and paste the generated schema into JSON Schema. Select Validate JSON. The observed report is invalid because the second array entry is not an integer.

The path $[1] identifies the second element, using zero-based indexing. The type keyword names the failed rule. This is useful feedback: the draft is too restrictive for the supplied sample. It is not evidence that the original JSON syntax is broken.

JSON Schema Validator reporting a type error at the second array item
The report rejects the string under integer items. The full keyword list can be scrolled in the tool.
Relevant excerpt of the observed validation reportjson
{
  "valid": false,
  "errors": [
    {
      "path": "$[1]",
      "keyword": "type",
      "message": "Expected integer, received string."
    }
  ]
}

Decide what the array is supposed to contain

Do not repair the draft simply by removing every rule until the sample passes. First decide whether the string is valid application data or an error in the sample. If all entries should be integers, correct the data. If a mixed array is intentional, choose a schema and validator that express the intended alternatives.

One sample cannot distinguish an exceptional error from an allowed variation. Collect representative valid cases and invalid cases before deciding the contract. Include empty arrays, missing values and entries later in the array rather than assuming the first value tells the whole story.

Review required keys and additional properties

For objects, the generator makes every observed key required and sets additionalProperties to false. A sample containing id and name therefore produces a draft that expects both and rejects extra keys. This can be useful for a strict record, but it can be wrong for optional fields or extensible objects.

Nested objects are inferred in the same way. A single null creates a null type, not a nullable alternative to the value you expected. Integer versus number depends on the observed number. Dates, ranges, enums and business meanings are not inferred just because a string or number looks familiar.

Structural shape produced from an object sample with id and name; review the required and extension decisions.json
{
  "type": "object",
  "properties": {
    "id": {
      "type": "integer"
    },
    "name": {
      "type": "string"
    }
  },
  "required": [
    "id",
    "name"
  ],
  "additionalProperties": false
}

Understand the local validator's boundary

The local validator checks implemented rules such as types, required keys, item structure and certain bounds. A keyword list is not proof of complete dialect support. Unknown formats or reference behavior should not be treated as enforced rules; use a standards-capable validator for the schema features your production contract requires.

Its const and enum object comparisons are also order-sensitive in the current implementation. Objects with the same keys and values in a different serialized order can therefore behave differently from a standards-aware semantic comparison. Do not use a green local result as the only gate for an API or data migration.

Build a small acceptance test set

Keep a representative valid record beside the schema, then create deliberate failures: remove a required field, change a number to a string, add an unexpected property and vary array items. Record which rule should reject each case. This turns a generated draft into a reviewable decision rather than an unexplained file.

After editing rules, repeat the cases with your intended production validator. Confirm that optional fields, extra keys and nullable values behave as agreed. Preserve the original sample and document why each constraint exists; a schema's purpose is to express those decisions, not just resemble one example.

Apply the steps from this guide directly in your browser.

Frequently asked questions

Can the generated schema reject the same JSON used to create it?

Yes. The observed [1,"two"] example produces integer items from the first element and rejects the later string when validated. Inspect mixed arrays and the intended data contract.

Are all observed object keys required?

The current generator makes the sample's object keys required and sets additionalProperties to false. Review whether those decisions fit optional or extensible data.

Does the draft 2020-12 label prove complete validation?

No. It identifies the emitted vocabulary. Inference and the local subset validator do not establish complete dialect, reference, format or business-rule support.

What should I test before using the schema in an application?

Test representative valid records and deliberate failures, including missing fields, wrong types, extra keys, empty or mixed arrays and nulls. Use the validator and dialect your application actually relies on.