{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"active": {
"type": "boolean"
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
},
"profile": {
"type": "object",
"properties": {
"age": {
"type": "integer"
},
"city": {
"type": "string"
}
},
"required": [
"age",
"city"
]
}
},
"required": [
"id",
"name",
"active",
"tags",
"profile"
]
}About JSON to JSON Schema
JSON Schema is the de-facto standard for describing and validating JSON structures, but writing one by hand is tedious and error-prone. This tool parses your sample JSON locally in the browser and infers a Draft-07 schema: each field's type, object properties and required lists, and array item types. All inference is local with nothing uploaded.
Inferring a structure from instances is induction: you get "this sample looks like this", not "the data must". The result therefore carries sampling bias — fields that happened to appear become required, ones incidentally missing become optional, and real constraints such as ranges or maximum lengths are invisible in a sample. Inference is only a starting point that a person must then complete.
Arrays distort inference most easily because one sample usually shows a single element shape. The same array may hold objects in some records and strings or numbers in others, while tools commonly take the first element or an intersection that mismatches reality. With arrays in the structure, infer from several samples or confirm by hand whether the element type is single.
Format constraints — dates, e-mail addresses, identifiers — cannot be derived reliably from appearance and must be added by hand. The same characters may look like a date while actually being an identifier that happens to match, and conversely a date-bearing field may hold only hard-to-recognise forms in the sample. A person who knows the business should declare formats, not pattern matching.
Extracting repeated shapes into reusable definitions is what makes the result maintainable. Inlining one shape in several places means every adjustment must be mirrored everywhere and is easily missed, whereas a referenced definition localises the change and guarantees consistency. Tidy the structure after generating and lift the repeats out, even though the file looks more complex.
Strictness must match purpose, since tighter constraints reject legitimate data more readily. For loose validation, declaring structure and types is enough and stays tolerant as data evolves, while an external contract also fixes required fields, enums and formats at the cost of updating the contract for every extension. One dataset may need descriptions at two levels.
Finally, keep the result in version control and reconcile it against real data. Data shapes change with the business, and a description file left unupdated becomes a stale document that is worse than none, because users trust it. Re-infer and re-review by hand after the sample changes as a fixed step, rather than generating once and forgetting.
How It Works Internally
Inferring a schema from samples yields only the constraints the sample happened to satisfy, whereas a schema exists to describe invariants. The result is therefore often too loose (required fields marked optional) or too strict (one observed value written as an enum). Treat it as a starting point, then tighten required fields and add ranges and formats by hand.
The sample {"id":1,"tags":["a"]}Infers an integer id and a string array for tags, but required fields cannot be inferred from one sample and must be added by handMeasured on This Page
| Self-consistency | Basic types, null/nesting and the empty array all validate their own source, with number/string/boolean/array/object inferred correctly |
|---|---|
| Empty array declaration | {"arr":[]} offers no element to infer, so only type=array is declared — missing information rather than a defect |
| Big integer as string | 9007199254740993 yields type=string rather than integer, because JS cannot hold it exactly; declaring integer would let a validator accept a truncated value |
| Handling null | null contributes no type information and the field stays optional rather than entering required, since one null would otherwise invalidate the whole schema |
The hard part of schema inference is not the type mapping but the boundaries: an empty array has no element to infer items from, null carries no type, and the same field may be a number in one sample and a string in another. This page errs toward declaring less rather than declaring wrongly — under-declaring only relaxes validation, while a wrong declaration rejects legitimate data. The 19-digit integer as string follows the same principle: integer looks more 'correct', yet the validator already holds …992, so it would check one corrupted number against another. For exact checks, send such values as strings from the source.
How to Use
- Open "JSON to JSON Schema"
- Paste the sample JSON to describe
- Set the schema title
- Choose whether fields go into required
- Click the copy icon to take the Draft-07 schema
Use Cases
- Validate API contracts — Generate a schema for request/response payloads to validate data at runtime and reject malformed input.
- Validate forms and config — Turn a config sample into a schema and validate user input or config files with libraries like ajv.
- Bootstrap OpenAPI — Get a schema quickly as the basis for components in OpenAPI/Swagger docs.
- Data quality gates — Use the schema as an entry check in data pipelines so downstream consumers get stable structures.
- Align team structures — Use the schema as a shared contract between frontend and backend to cut communication overhead.
- Generate UI forms — Derive a JSON Schema from a data sample to drive auto-generated form components with validation rules.
- Define database validation — Create a schema from a sample document to enforce shape constraints on JSON columns in PostgreSQL or MongoDB.
FAQ
Which schema version is produced?
It outputs JSON Schema Draft-07 by default, compatible with mainstream validators like ajv. Adjust to another draft after copying if needed.
Are all fields marked required?
Controlled by the "Mark required" toggle. When on, every field present in the sample is listed as required; for optional fields, turn it off and adjust manually.
How are mixed array types handled?
When array elements differ, the tool lists each possible sub-schema under anyOf to match the real data.
Are integers and floats distinguished?
Yes. Whole numbers infer to integer and decimals to number for more precise validation.
Can it validate my JSON?
This tool generates the schema. You can then validate real data with libraries like ajv or jsonschema, or an online validator.
The generated schema is too loose — how do I tighten it?
Because the tool only infers from your sample, a null in the sample widens the field to anyOf including null, and an empty object {} can only infer to {} which accepts anything — the leaner the sample, the looser the schema. To tighten it, edit the generated schema on the right: drop redundant null/type branches from anyOf, add properties and required to empty objects, and attach constraints like a pattern regex, minimum/maximum, or maxLength per business rules. Then validate with real samples to see which values still pass that shouldn't, and keep narrowing until only legitimate values are allowed.
How do I add enums and constraints like uniqueItems?
Edit the field's schema object directly in the editor on the right: add pattern or minLength for strings, minimum/maximum for numbers, an enum array for fixed value sets, and uniqueItems:true for arrays whose entries must not repeat. You can then copy the schema into a validator such as ajv and test it against a few real arrays to confirm the constraints behave as intended. More specific constraints catch more bad data at runtime, and the result doubles as an interface contract and collaboration doc that reduces type-mismatch rework during integration.
How is the required list in the generated schema determined?
From the samples: a field present in every object is required, otherwise optional. Fewer samples mean cruder inference. Feed multiple real responses, or review the required list by hand — the schema is a contract and one wrong field trips every consumer.