How to Generate JSON Schema from JSON Data: A Complete Guide

JSON Schema is a powerful vocabulary that allows you to annotate and validate JSON documents. Instead of writing schemas manually from scratch — a process that is error-prone and time-consuming — you can use a json schema generator to infer the schema directly from your data. This guide covers every approach: online tools, manual writing, and programmatic generation, so you can choose what fits your workflow.

What is JSON Schema?

JSON Schema describes the structure of JSON data. It defines which fields are required, what data types they should be, and what constraints they must satisfy. A schema itself is written in JSON, making it both human-readable and machine-parseable.

Here is a minimal schema for a user profile:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "age": { "type": "integer", "minimum": 0 },
    "email": { "type": "string", "format": "email" }
  },
  "required": ["name", "email"]
}

With this schema, a validator can confirm that {"name": "Alice", "email": "alice@example.com"} is valid, while {"name": "Bob"} (missing email) or {"age": "thirty"} (wrong type) are rejected.

Generating a schema from real data saves you the effort of enumerating every field and type by hand. Instead, you feed a representative JSON sample into a json schema generator and get a complete schema back in seconds.

Method 1: Use an Online Generator

The fastest way to generate json schema from json is with an online tool. No installation, no dependencies — paste your JSON and get the schema.

Step-by-Step with devformatters

  1. Open the JSON Schema Generator in your browser.
  2. Paste your sample JSON into the left-hand input panel.
  3. Select the desired JSON Schema draft (Draft-07 is recommended for most projects).
  4. Click Generate Schema. The generated schema appears instantly in the right-hand output panel.

Let's walk through a real example. Suppose you have this product listing:

{
  "product": {
    "id": 1024,
    "name": "Wireless Bluetooth Headphones",
    "price": 49.99,
    "inStock": true,
    "tags": ["electronics", "audio", "new-arrival"],
    "reviews": [
      {
        "user": "tech_reviewer",
        "rating": 5,
        "comment": "Great sound quality for the price."
      }
    ]
  }
}

The generator produces a schema like this:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "product": {
      "type": "object",
      "properties": {
        "id": { "type": "integer" },
        "name": { "type": "string" },
        "price": { "type": "number" },
        "inStock": { "type": "boolean" },
        "tags": {
          "type": "array",
          "items": { "type": "string" }
        },
        "reviews": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "user": { "type": "string" },
              "rating": { "type": "integer" },
              "comment": { "type": "string" }
            }
          }
        }
      }
    }
  }
}

The tool infers nested objects, arrays with their item types, and every primitive field automatically. You can copy the output and drop it directly into a JSON Schema validator, an API gateway, or a documentation generator.

Why Use an Online Generator?

  • Zero setup — works in any browser, no packages to install.
  • Instant feedback — tweak the input JSON and regenerate in one click.
  • Draft selection — switch between Draft-04, Draft-07, and 2020-12 to match your project's requirements.
  • No data leaves your browser — the generation runs client-side.

Method 2: Manual Schema Writing

While a generator handles most cases, understanding manual schema writing is important when you need to add constraints that cannot be inferred from data alone — like regex patterns, conditional logic, or cross-field references.

Nested Objects

For a deeply nested structure, define schemas inside properties recursively:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "company": {
      "type": "object",
      "properties": {
        "name": { "type": "string" },
        "address": {
          "type": "object",
          "properties": {
            "street": { "type": "string" },
            "city": { "type": "string" },
            "zip": { "type": "string", "pattern": "^\\d{5}$" }
          },
          "required": ["street", "city"]
        }
      },
      "required": ["name"]
    }
  }
}

Arrays with Complex Items

When an array holds objects, you specify the item schema inside items:

{
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "sku": { "type": "string" },
      "quantity": { "type": "integer", "minimum": 1 }
    },
    "required": ["sku", "quantity"]
  },
  "minItems": 1
}

Adding Constraints After Generation

The best workflow is to use a json schema generator as a starting point and then layer on manual constraints. The generator gives you the shape; you add the rules. For example, after auto-generating the schema for a registration form, you might add:

{
  "password": {
    "type": "string",
    "minLength": 8,
    "pattern": "^(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)"
  }
}

Method 3: Generate from Code

When you are building automated pipelines, you will want to generate schemas programmatically.

Node.js with jsonschema

The jsonschema npm package can infer schemas from JavaScript objects:

const { Validator, Schema } = require('jsonschema');
const inferSchema = require('jsonschema-inferrer');

// Or use the built-in infer method:
const schema = {
  type: 'object',
  properties: {}
};

const data = {
  title: "Hello World",
  views: 300,
  published: true
};

// Using the inferrer package for automatic detection
const inferred = inferSchema(data);
console.log(JSON.stringify(inferred, null, 2));

For a more robust solution, use the generate-schema package:

const generateSchema = require('generate-schema');

const data = [
  {
    name: "Alice",
    age: 30,
    metadata: { role: "admin" }
  }
];

const schema = generateSchema.json('User', data);
console.log(JSON.stringify(schema, null, 2));

Python with genson

The genson library (Genson — Generate Schema) is the most mature Python option:

pip install genson
from genson import SchemaBuilder

builder = SchemaBuilder()
builder.add_object({
    "title": "Hello World",
    "views": 300,
    "published": True
})

schema = builder.to_schema()
print(schema)
# {'type': 'object', 'properties': {'title': {'type': 'string'}, 'views': {'type': 'integer'}, 'published': {'type': 'boolean'}}}

You can also feed multiple samples to build a more complete schema:

builder = SchemaBuilder()
builder.add_object({"status": "active", "code": 200})
builder.add_object({"status": "inactive", "code": 404, "error": "Not found"})

schema = builder.to_schema()
print(schema)
# {'type': 'object', 'properties': {'status': {'type': 'string'}, 'code': {'type': 'integer'}, 'error': {'type': 'string'}}}

Genson automatically merges the two samples so error becomes optional (not listed in required).

JSON Schema Drafts: 04 vs 07 vs 2020-12

Choosing the right draft matters because different validators and code generators target different versions.

FeatureDraft-04Draft-072020-12
Release201320182020
$idSupportedSupportedRedesigned
const
if/then/else
examples
unevaluatedProperties
prefixItems (tuple)
Default URIjson-schema.org/draft-04/schema#json-schema.org/draft-07/schema#json-schema.org/2020-12/schema#

Draft-04 is legacy. Many older tools still use it, but you should migrate off it if possible.

Draft-07 is the current sweet spot. It has wide validator support and adds if/then/else conditional validation and const. If you are unsure which draft to pick, choose Draft-07.

2020-12 is the latest official release. It introduces unevaluatedProperties (properties that survive allOf evaluations) and first-class tuple typing with prefixItems. Use this for new greenfield projects where your entire toolchain supports it.

Most online generators, including the devformatters JSON Schema Generator, let you switch drafts so you can target the version your project requires.

Validating JSON Against Your Schema

Generating a schema is only half the workflow. You also need to validate your data against it.

Using the devformatters Validator

  1. Navigate to the JSON Schema Validator.
  2. Paste your schema into the left panel.
  3. Paste your JSON data into the right panel.
  4. Click Validate. Errors, if any, are highlighted with precise line numbers and descriptions.

Example validation error:

// Schema expects "age" to be an integer
{
  "name": "Alice",
  "age": "thirty"  // <-- Validation error: type should be integer
}

The validator would return something like:

Error at /age: type should be integer, got string

Programmatic Validation

If you prefer to validate in code, pair the schema you generated with a validator library:

  • Node.js: ajv (the fastest JSON Schema validator)
  • Python: jsonschema (pip install jsonschema)
  • Go: gojsonschema
  • Java: everit-json-schema
const Ajv = require('ajv');
const ajv = new Ajv();

const schema = {
  type: 'object',
  properties: {
    age: { type: 'integer' }
  }
};

const validate = ajv.compile(schema);
const valid = validate({ age: 'thirty' });
console.log(valid); // false
console.log(validate.errors);

Common Schema Patterns

A json schema generator handles the straightforward cases, but real-world APIs often require more sophisticated patterns.

Nullable Values

By default, { "type": "string" } rejects null. To allow null:

{
  "type": "string",
  "nullable": true
}

In Draft-07 and later you can also use:

{
  "anyOf": [
    { "type": "string" },
    { "type": "null" }
  ]
}

oneOf / anyOf

When a field can be one of several types:

{
  "oneOf": [
    {
      "type": "object",
      "properties": {
        "kind": { "const": "email" },
        "address": { "type": "string", "format": "email" }
      }
    },
    {
      "type": "object",
      "properties": {
        "kind": { "const": "phone" },
        "number": { "type": "string", "pattern": "^\\+?[1-9]\\d{1,14}$" }
      }
    }
  ]
}

oneOf ensures exactly one match; anyOf allows one or more. These are invaluable for modeling union types that cannot be inferred automatically from a single JSON sample. After you generate json schema from json samples representing each variant, you can manually combine them into a oneOf wrapper.

Conditional Validation (if / then / else)

Draft-07 introduced the most practical feature for real-world APIs: conditional rules.

{
  "type": "object",
  "properties": {
    "country": { "type": "string" },
    "postalCode": { "type": "string" }
  },
  "if": {
    "properties": { "country": { "const": "US" } }
  },
  "then": {
    "properties": {
      "postalCode": { "pattern": "^\\d{5}(-\\d{4})?$" }
    }
  },
  "else": {
    "properties": {
      "postalCode": { "pattern": "^[A-Z]\\d[A-Z] \\d[A-Z]\\d$" }
    }
  }
}

When country is "US", the postal code must be a ZIP code; otherwise it must match the Canadian format. This kind of logic cannot be derived from data alone, so you write the if/then/else branch after your initial schema generation pass.

Conclusion

JSON Schema turns unstructured JSON into documented, validated data. Whether you are designing an API, writing configuration files, or building a form with dynamic validation, a json schema generator saves you hours of manual typing.

Here is the workflow we recommend:

  1. Collect a representative sample of your JSON data.
  2. Run it through the JSON Schema Generator to get a baseline schema.
  3. Add manual constraints — regex patterns, oneOf unions, conditional rules — that the data alone cannot express.
  4. Validate your data against the finished schema using the JSON Schema Validator or a library like AJV.

The combination of automated generation and hand-tuned rules gives you production-ready schemas in minutes, not hours. Bookmark the tools and come back whenever you need to generate json schema from json fast.