{
"color": "red",
"count": 2
}A sample payload is the fastest way to sketch a contract, as long as you remember it only describes what that sample contains. JSON Schema from a sample walks the value and emits a draft 2020-12 schema. Objects become type object, with a properties map and a required array of every key on that object. Strings, integers, numbers, booleans, and null are typed from the value. Arrays become type array.
Two different object shapes inside one array become anyOf, not a single object with optional keys. A key that is absent from the sample is absent from the schema. count: 2 is an integer. A decimal would be a number. The page does not invent formats, enums, or minimums. It records the shape you pasted.
{"color":"red","count":2}
{ "$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object", "properties": { "color": { "type": "string" }, "count": { "type": "integer" } }, "required": ["color", "count"] }
The inferred schema is a faithful picture of one sample, including the required list, so you can see what the example accidentally made mandatory.
The sample included them. The inference has no second document to show a key that can be omitted. Remove a key from required if your API allows it to be absent.
The value 2 has no fractional part. A sample of 2.5 would be typed as number. JSON to TypeScript types the same 2 as number, because TypeScript has no integer type in this output.
Each distinct item schema is kept. One shape produces a single items schema. Two shapes produce anyOf under items.
https://json-schema.org/draft/2020-12/schema. The document is only as strict as the sample.
It writes a schema. It does not check a second document against that schema. JSON validator checks syntax of the text you paste.