What is inferred from a sample
The generator walks your JSON and records what it finds, producing draft-07 output:
{"age": 30}
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": { "age": { "type": "integer" } },
"required": ["age"]
}
Whole numbers become integer and others become number. Objects are followed to any depth, and a top-level array is handled the same way as a top-level object.
Arrays are compared item by item
Every element of an array is inspected, not just the first. If one item has a note field and another does not, note appears in properties but stays out of required. If one item has a whole number where another has a decimal, the field becomes number. Mixed types produce a list such as ["integer", "string"].
So the best sample is an array with several realistic records, including ones with optional fields left out. A single perfect record teaches the generator nothing about what can vary.
Formats are judged from values
A format is added only when the value itself looks right: email, date, date-time, uuid, uri or ipv4. Field names are ignored, so an "id" holding "abc" stays a plain string. Be aware that most validators treat format as an annotation by default and do not reject a bad value unless the option is switched on. In Ajv that requires the separate ajv-formats package.
What to add by hand
A sample cannot reveal the rules behind the data. Before relying on the result, consider adding:
- enum for a field limited to fixed values, such as a status.
- minimum and maximum for numbers with known bounds.
- minLength, maxLength or pattern for strings.
- additionalProperties: false if unknown fields should be rejected.
- A null type alongside the original, for any field that can be empty.
Without additionalProperties, a schema allows extra keys, so a typo like "emial" passes as long as the required fields are also present. Test the finished schema against both good and deliberately bad records.