Sample JSON from Schema
Generate example JSON from a JSON Schema: required and optional properties, enums, formats, ranges, arrays and local $ref, checked with Ajv. Runs in your browser.
How to use Sample JSON from Schema
- 1Paste a JSON Schema or open a .json file.
- 2Choose whether to include optional properties, or turn on Randomise values.
- 3Check the validity badge and notices, then copy or download sample.json.
What makes it better
- Honours type, properties, required, enum, const, default and examples
- Formats: date, date-time, email, uri, uuid, ipv4 and hostname
- Minimum, maximum, exclusive bounds, multipleOf, minLength, maxLength, minItems and maxItems
- Local $ref to #/definitions and #/$defs, with cycle and depth guards
- Validated against the schema with Ajv, shown as a badge
About Sample JSON from Schema
Sample JSON from Schema reads a JSON Schema and writes an example document that follows it. It handles types, properties, required, enum, const, default, formats, numeric and length limits, array sizes, allOf, the first branch of oneOf and anyOf, and local $ref. The example is checked against your schema with Ajv, and the output is the same every time unless you switch on random values.
Common uses
- Create a sample request or response body for API documentation from its schema.
- Produce a fixture for a unit test without writing the JSON by hand.
- Check that a schema allows what you expect by looking at an example it produces.
- Seed a mock server or a form prototype with schema-shaped data.
Example
Input
{
"type": "object",
"required": [
"id",
"email",
"role"
],
"properties": {
"id": {
"type": "integer",
"minimum": 1
},
"email": {
"type": "string",
"format": "email"
},
"role": {
"enum": [
"admin",
"editor",
"viewer"
]
},
"active": {
"type": "boolean"
},
"tags": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 2
}
}
}Result
{
"id": 1,
"email": "[email protected]",
"role": "admin",
"active": true,
"tags": [
"string",
"string"
]
}Good to know
- A pattern is not turned into a matching string. If the schema has examples, a default or an enum those are used; otherwise a placeholder is written and a notice says the pattern was not honoured.
- oneOf and anyOf use their first branch and say so. Remote $ref values are refused and never fetched; only #/definitions/... and #/$defs/... are resolved.
- Some keyword combinations cannot be satisfied automatically, such as unique items from a one-value enum. The badge then reports what failed so you can edit the example.
Sample JSON from Schema FAQ
Why does the example always look the same?
Output is deterministic by default, so the same schema gives the same example, which suits documentation and tests. Turn on Randomise values for different numbers, dates, enum choices and array lengths each time you press Regenerate.
What happens with a regular expression in pattern?
The generator does not build strings from regular expressions. It uses an example, default or enum if the schema has one, and otherwise inserts a placeholder and shows a warning that the pattern was not honoured.
Which $ref forms are supported?
References that start with #/definitions/ or #/$defs/ in the same schema. Remote URLs and other pointers are not fetched or resolved and leave null with a notice. A schema that refers back to itself stops at the first repeat.
Why does the badge say the example is invalid?
The generator takes the simplest route through each keyword, and some combinations, such as patterns with minLength or conflicting allOf branches, cannot be satisfied that way. The badge lists the first failures reported by Ajv so you can adjust the schema or edit the example.
How are oneOf, anyOf and allOf treated?
allOf branches are merged into one schema. For oneOf and anyOf the first branch is used and a notice reports how many were available. Pick a different branch by reordering the list in the schema.