JSON to JSON Schema Converter – Free, Online, Private
Infer a draft 2020-12 JSON Schema from a sample document. Nested objects become $defs, and keys missing from some array elements are not required.
How to convert JSON to JSON Schema
- Paste one JSON value that shows the fields you care about.
- Read the schema. The root object is inline and nested objects are $refs.
- Relax additionalProperties or required if the sample was narrower than the API.
How the schema is inferred
A JSON object becomes an object schema with additionalProperties set to false and required set to the keys the sample actually contained. A nested object is moved into $defs and referenced, so the document stays readable when the same shape would otherwise be inlined several times. Arrays use items. If every element is an object, the item schema is the merge of those objects: a key is required only when every element has it, and a key present on only some elements stays a property but drops out of required. An empty array has no items schema at all, because there is nothing to infer. Null becomes type null. A value that is null in one element and a string in another becomes a type array of string and null. Integers and fractional numbers collapse to number when both appear.
Draft 2020-12 on purpose
The $schema keyword points at the 2020-12 meta-schema. $defs is used instead of the older definitions keyword. type arrays express nullable primitives, which 2020-12 allows. The page does not emit OpenAPI's nullable: true, and it does not wrap the document in a components section. If you need an OpenAPI schema, copy the definitions across and adjust the dialect. format is never guessed. An email-shaped string is still type string. Guessing format: date-time from a single ISO string produces schemas that reject values the API accepts. Leave format to a human who has read the contract.
Worked example
The sample user is a root object titled Root. id is integer, name and the address fields are string, active is boolean, and tags is an array of strings. address is a $ref to $defs/Address, whose required list is city and zip, with additionalProperties false. Nothing is optional, because the sample is one complete object. Paste an array of two users and omit zip on the second if you want zip to disappear from required. The schema will still list zip under properties. That is the difference between 'the key may be absent' and 'the key cannot exist'. additionalProperties false means a later field such as country fails validation until you add it or set the flag to true.
Closed objects are a choice
additionalProperties false is useful when you want a fixture to catch typos, and it is hostile when an API adds fields without a version bump. The flag is set because a sample cannot name the keys it did not include, and a silent additionalProperties true would hide that. Change it if your clients should ignore unknown fields. The schema is not a substitute for an authored contract. One payload cannot reveal a numeric string, a discriminated union, or a field that is optional but happened to be filled in. Compare the result with the TypeScript inference if you want the same rules in a type system. Both pages run in the browser. The document is not uploaded.
Frequently asked questions
Which draft is this?
JSON Schema draft 2020-12. $defs and type arrays match that draft, not draft-04.
Why is additionalProperties false?
So a key the sample never showed is a validation error until you decide otherwise. Delete the flag if unknown keys should pass.
Does an empty array get an items schema?
No. There is no element to infer. Put one example element in the array and convert again.
Will email strings get format email?
No. Formats are not guessed from examples. Add format yourself when the contract requires it.