From JSON Schema to Sample JSON: Mock Data, Examples and Edge Cases

By Wesley · · 6 min read

A JSON Schema says what data is allowed. It does not show what the data looks like, and that is often what a teammate, a frontend prototype or a test actually needs. Generating a sample document from a schema fills that gap. It is a simple idea with a few sharp edges, mostly around the difference between "has the right shape" and "is valid".

JSON Schema
Sample JSON
Mock API response
Example in API docs
Test fixture
Default form state
One schema, one generated document, several consumers.

How a generator picks values

A generator walks the schema the same way a validator does, but instead of checking a value at each node it produces one. The type keyword does most of the work:

SchemaTypical outputThis site’s generator
type: stringA placeholder string"sample string", or a guess from the property name (name → "John Doe", email → "[email protected]")
type: number / integerA fixed or random number11
type: booleantruetrue
type: arrayOne or more items built from itemsTwo items
type: objectOne value per entry in propertiesEvery listed property, recursively
No typeDepends on other keywordsnull
Schema
{
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "email": { "type": "string" },
    "age": { "type": "integer" },
    "roles": { "type": "array", "items": { "type": "string" } }
  },
  "required": ["name", "email"]
}
Generated sample
{
  "name": "John Doe",
  "email": "[email protected]",
  "age": 11,
  "roles": ["sample string", "sample string"]
}

Notice that age and roles are present even though they are not required. Most generators include every property by default, because a sample that shows all fields is more useful as documentation.

Keywords beyond type

Type-only generation gives you the right shape. Whether the result is valid depends on the other keywords in the schema. A structural generator like the one on this site reads type, properties and items and ignores the rest, so you may need to adjust the output by hand:

KeywordWhat a correct sample needsEasy fix
enum, constOne of the listed valuesReplace the placeholder with the first enum value
default, examplesNothing, but they are the best values to showCopy them in; they were written by someone who knows the domain
formatA string in that format (date-time, uuid, uri)Use a realistic literal such as 2026-01-15T09:30:00Z
minimum, maximum, multipleOfA number inside the rangePick the minimum
minLength, patternA string that matchesWrite one by hand; patterns are hard to invert automatically
minItems, uniqueItemsEnough distinct itemsVary the repeated placeholder
oneOf, anyOfA value matching one branchChoose a branch and generate from it
allOfA value matching every branchMerge the branches first
$refThe referenced schema, resolvedInline the definition from $defs
Shape versus validity
A sample can have every field in the right place and still fail validation because of an enum or a pattern. If the sample will be published in API docs or used as a fixture, validate it.

Always validate the sample

The check takes a few lines and catches the mismatch before a reader copies a broken example. With Ajv:

JavaScript
import Ajv2020 from 'ajv/dist/2020';

const validate = new Ajv2020({ allErrors: true }).compile(schema);
if (!validate(sample)) {
  for (const err of validate.errors) {
    console.log(err.instancePath || '(root)', err.message);
  }
}

Running this in CI for every example in your documentation is one of the cheapest ways to keep docs and schema from drifting apart.

Choosing the right kind of generator

  • A structural sample (this site's tool) is enough when you need to see the shape, scaffold a fixture or hand a frontend developer something to render.
  • A keyword-aware faker such as json-schema-faker respects formats, ranges, patterns and enums and produces random valid data. Use it for mock servers and load tests.
  • Property-based testing libraries (for example hypothesis-jsonschema in Python) generate many valid documents, including awkward ones: empty strings, zero, maximum lengths. Use them to test the code that consumes the data.
  • Hand-written examples in the schema itself are still the best documentation. A generator is the fallback for the schemas nobody annotated.

Edge cases worth knowing

  • Recursive schemas. A tree node that references itself would generate forever. Generators need a depth limit; when writing one, stop at optional recursive properties first.
  • Contradictory schemas. { "type": "string", "enum": [1, 2] } accepts nothing. No sample exists, and a failed generation is a useful signal that the schema is wrong.
  • Optional versus required. For negative tests, generate the full sample and then delete one required key at a time. Each variant should be rejected.

Try it

JSON Schema to JSONPaste a schema and get a sample document with the same structure.

Starting from data instead of a schema? See inferring a JSON Schema from sample JSON.