Generate a JSON Schema from JSON: type, required and format Rules

Last updated: · 8 min read

JSON Schema is a language, itself written in JSON, for stating what shape a JSON document must have: which fields are present, what type each one is, what the items of an array look like. It is used to validate API requests, check configuration files and generate forms. Writing a schema from scratch is tedious, so the JSON Schema Generator derives one that follows draft 2020-12 from a sample document you already have. That schema describes what the sample contains, not the rules of your data: a sample alone cannot decide whether a field is mandatory or which range of values it accepts. This guide explains the inference rules, how the required and format lines are decided, and what you should tighten by hand before relying on the schema. The output shown is the tool's real result.

Formula

Rules:
1. The schema starts with "$schema": "https://json-schema.org/draft/2020-12/schema".
2. Every value gets a type: null, boolean, integer, number, string, array or object.
3. Numbers are classified by spelling: digits only → integer; a decimal point or an exponent (9.5, 1.0, 1e3) → number. If both occur in the same place, the result is number.
4. Objects: properties lists the keys in order of first appearance. required contains the keys present in every merged sample.
5. Arrays: all elements are merged into a single items schema. If the elements have different types, items uses anyOf.
6. null: next to one other type it is written in the short form "type": ["string", "null"]; next to several types it becomes a separate {"type": "null"} branch inside anyOf.
7. format is added only when every string seen in that place strictly matches the same format: date-time, date, email, uri or uuid.
8. Never emitted: additionalProperties, enum, minimum/maximum, minLength/maxLength, pattern, title and description.

Input:
[{"id":1,"email":"ada@example.com","created":"2024-05-01T10:00:00Z","nick":null,"score":9.5},{"id":2,"email":"bob@example.com","created":"2024-06-15T08:30:00Z","nick":"bob","score":7,"site":"https://example.com/bob"}]

Output:
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "id": {
        "type": "integer"
      },
      "email": {
        "type": "string",
        "format": "email"
      },
      "created": {
        "type": "string",
        "format": "date-time"
      },
      "nick": {
        "type": [
          "string",
          "null"
        ]
      },
      "score": {
        "type": "number"
      },
      "site": {
        "type": "string",
        "format": "uri"
      }
    },
    "required": [
      "id",
      "email",
      "created",
      "nick",
      "score"
    ]
  }
}

This is an inference algorithm, not a formula. The tool walks the document, records which types occur at each position and finally translates that into schema keywords. The objects inside an array count as one set of samples: only keys present in every object go into required, while the rest stay in properties and are therefore optional. The keywords used are defined in the JSON Schema Core and Validation documents. type, properties, required, items and anyOf decide the validation result; format, in draft 2020-12, is by default only an annotation.

What the inputs mean

JSON
The sample document the schema is inferred from. A single object works, but several different records in one array give a much better result: only then can the tool see which fields are sometimes missing and which can be null.

Worked example: Inferring a schema from two user records

  1. The input is an array of two objects. The root becomes "type": "array" and the two objects are merged into a single items schema.
  2. id is a number without a decimal point in both records: "type": "integer". score is 9.5 in one record and 7 in the other; because a decimal spelling occurs, the result is "type": "number".
  3. Both email values strictly match the e-mail format, so "format": "email" is added. The created values are RFC 3339 timestamps with a time zone: "format": "date-time".
  4. nick is null in one record and a string in the other. When null occurs next to a single type, the short form is used: "type": ["string", "null"].
  5. site exists only in the second record. It appears in properties with "format": "uri" but is left out of required, which lists only the keys both records share: id, email, created, nick and score.
  6. nick is in that list even though it may be null: required demands that the key is present, not that it has a value.
  7. A single sample changes the result: for {"id":1,"nick":null}, nick gets "type": "null" and both fields become required. That schema rejects a string in nick; from one sample the tool cannot know what else the field may hold.
  8. A non-strict format and a mixed array: in {"when":"2024-02-30","values":[1,"a"]}, 2024-02-30 is not a real date, so when gets no format and is just "type": "string"; for the items of values, the integer and string alternatives are listed under "anyOf": [.

Result: The output is an array schema that defines the properties id, email, created, nick, score and site and requires the first five. Before using it, decide for yourself whether extra fields are allowed (additionalProperties), what the value limits are and which fields are truly mandatory.

📐 JSON Schema Generator

Free, no sign-up; your data stays in your browser.

Open the calculator →

How to read the result

A schema inferred from one sample is only a starting point. The tool generalises what it saw: a field present in every sample is treated as required, a field that was always a whole number becomes integer, and if every value looked like an e-mail address, format is added. In the real contract the same field may be optional, fractional or free text. The reverse also holds: the tool invents no constraints. Minimum and maximum values, lengths, patterns and sets of allowed values (enum) are never written. additionalProperties is not written either, so the schema accepts extra fields it does not define; if you want a strict contract, add "additionalProperties": false to the objects concerned. Be careful with format: in draft 2020-12 it does not validate by default and only carries information, so you may have to switch on format assertion in your validator separately.

Common mistakes

  • Mistaking the generated schema for the contract. It is the common shape of the records in the sample; it cannot know cases the sample lacks (a missing field, a null, another type) and can turn out stricter or looser than intended.
  • Confusing required with "cannot be null". required only demands that the key exists; whether the value may be null is decided by type. Optional but non-null fields and required but nullable fields are both valid.
  • Assuming a format line validates on its own. In draft 2020-12 format is an annotation by default; if you want formats checked, enable that in your validator.
  • Expecting extra fields to be rejected. Because additionalProperties is not written, keys the schema does not define are valid, and a misspelt field name passes unnoticed.
  • Leaving a field that was null in a one-record sample as it is. "type": "null" accepts nothing but null; add the field's real type, for example ["string", "null"].
  • Expecting values written like 1.0 to come out as integer. The tool writes number as soon as it sees a decimal point. In JSON Schema a number with a zero fractional part also counts as an integer, so if the field really holds whole numbers you can change the type to integer by hand.
  • Expecting an item schema from an empty array. For "tags": [] only "type": "array" is written; the element type has to be added by hand.

Frequently asked questions

How do required and optional fields work in JSON Schema?

properties says what a field must look like but does not force it to exist; by default every field is optional. To make a field mandatory, you list its name in the object's required array. The tool puts into that array the keys present in every sample it merged.

Can a field be required and nullable at the same time?

Yes. required demands that the key is present in the document; a type list such as "type": ["string", "null"] allows the value to be null. The nick field in the example is exactly that: present in every record, with a value that may be null.

What is the difference between integer and number?

number accepts any number, integer only whole numbers. In JSON Schema a number with a zero fractional part, such as 1.0, also matches integer. The tool decides by spelling: a number written with digits only becomes integer, one with a decimal point or exponent becomes number.

When is format added and when not?

The tool recognises five formats: date-time, date, email, uri and uuid. A format is added when every string seen in that place matches strictly. An impossible date such as 2024-02-30, a timestamp without a time zone, or values in differing formats get no format.

Which draft is used, and does it work with my draft-07 validator?

The schema declares draft 2020-12. The keywords the tool uses (type, properties, required, items with a single schema, anyOf and format) mean the same in draft-07, so for an older validator it is usually enough to change the $schema line.

How do I forbid extra fields?

Add "additionalProperties": false to the object schema in question. The keyword makes keys that are not defined in properties invalid. The tool does not write it on its own, because a sample cannot show whether fields it does not contain are forbidden.

Is my JSON sent to a server?

No. The schema is generated in your browser. It is still good practice to replace personal fields before pasting samples that contain real customer data.

The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.

Sources

Choose your cookie preference to continue

Records the site needs to work (theme, language, favourites) stay in your browser.

This site is free and funded by ads.

With your consent, Google Analytics cookies measure visits and Google AdSense cookies are used to show ads; the data is processed on Google servers abroad.

If you reject, the site works just the same.

Cookie Policy · KVKK notice