Skip to content
uencoder
TUTORIAL

How to Generate a JSON Schema

Infer a JSON Schema from sample JSON data for validation and documentation.

Problem

You have JSON data and want to generate a schema that describes its structure for validation or API documentation.

Prerequisites

  • Understanding of JSON
  • Familiarity with JSON Schema concepts

Steps

  1. 1. Analyze the sample JSON

    The generator inspects the JSON to determine types, required fields, array item types, and nested object structures. It infers the schema from the data. Multiple samples can improve accuracy by detecting optional fields and enum values.

    Input
    {"name":"Alice","age":30}
    Output
    inferred structure
  2. 2. Generate the schema

    Produce a JSON Schema document with type constraints for each field. Strings get type string, numbers get type number or integer, arrays get items with the element type, and objects get properties with nested schemas. Mark fields present in all samples as required.

    Input
    sample JSON
    Output
    JSON Schema draft 2020-12
  3. 3. Refine and validate

    Review the generated schema and refine it: add constraints (minLength, maximum, pattern), identify optional vs required fields, and set enum values for fields with known options. Validate the original JSON against the schema to confirm it passes.

    Input
    schema + original JSON
    Output
    validation passes

Expected result

A JSON Schema that validates the sample data and can be used for runtime validation or API documentation.

Common errors

Schema is too permissive
A single sample cannot infer all constraints. Add more samples or manually refine the schema with tighter constraints.
Array item type is wrong
If the array contains mixed types, the schema may infer a union type. Provide more samples or specify the expected item type manually.

Troubleshooting

  • If validation fails against the generated schema, the schema may have over-constrained a field. Relax the constraint.
  • Use multiple samples to detect optional fields and enum values.

Security limits

  • Schema generation is a read-only analysis. No data is transmitted.

Related tools

Related glossary

Related tasks

Ready to run it?

References