Set JSON Schema Required Fields From Sample Records
A single record can make an optional key look mandatory. With strict mode off, the linked generator puts a key in required when it appears in every observed object at the same location. Use rows with and without optional values to give it a useful first draft.
Worked example
Inventory rows with an optional bin
[
{"id": "P-001", "bin": "A-7"},
{"id": "P-002"}
]Steps
- Paste the sample array into the generator with strict mode off. The second record deliberately omits the bin field.
- In the generated schema, look under items. Keep id in required to enforce the inventory contract, and keep bin in properties so its value is still checked when supplied.
- Validate the array, then remove id from one record and validate again. The original array should pass; the version missing id should fail at that record.
What to check
- Confirm items.required contains id and omits bin. If your application sends null for an empty bin, allow null explicitly or include that case when inferring the schema.
- A generator only knows what it sees. If an API identifies a field as mandatory, keep it required even if one input sample was incomplete.