From JSON Schema to Sample JSON: Mock Data, Examples and Edge Cases
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".
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:
| Schema | Typical output | This site’s generator |
|---|---|---|
type: string | A placeholder string | "sample string", or a guess from the property name (name → "John Doe", email → "[email protected]") |
type: number / integer | A fixed or random number | 11 |
type: boolean | true | true |
type: array | One or more items built from items | Two items |
type: object | One value per entry in properties | Every listed property, recursively |
| No type | Depends on other keywords | null |
{
"type": "object",
"properties": {
"name": { "type": "string" },
"email": { "type": "string" },
"age": { "type": "integer" },
"roles": { "type": "array", "items": { "type": "string" } }
},
"required": ["name", "email"]
}{
"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:
| Keyword | What a correct sample needs | Easy fix |
|---|---|---|
enum, const | One of the listed values | Replace the placeholder with the first enum value |
default, examples | Nothing, but they are the best values to show | Copy them in; they were written by someone who knows the domain |
format | A string in that format (date-time, uuid, uri) | Use a realistic literal such as 2026-01-15T09:30:00Z |
minimum, maximum, multipleOf | A number inside the range | Pick the minimum |
minLength, pattern | A string that matches | Write one by hand; patterns are hard to invert automatically |
minItems, uniqueItems | Enough distinct items | Vary the repeated placeholder |
oneOf, anyOf | A value matching one branch | Choose a branch and generate from it |
allOf | A value matching every branch | Merge the branches first |
$ref | The referenced schema, resolved | Inline the definition from $defs |
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:
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-jsonschemain 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
examplesin 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.