Home → JSON Regex Generator

JSON Regex Generator

SG
By Saurabh Goyal · Independent Software Developer
Builds JSON Web Tools · GitHub · About the author

Paste a JSON object and get a regular expression for every field — ready to paste into the JSON Schema pattern keyword. Below the tool: 14 tested patterns for common JSON field types, and the escaping rules that break most hand-written patterns.

About This Tool

The generator reads each field of a JSON object, infers what kind of value it holds, and emits an anchored regex for it. The output is ECMA 262 — the dialect JSON Schema's pattern keyword uses — so patterns paste straight into a schema or into JavaScript. Everything runs in your browser; no JSON is sent to a server. Free, no account required.

What the JSON Regex Generator Does

Give it one JSON object and it returns a field-by-field map of regex patterns — the tedious half of writing a JSON Schema by hand.

One Pattern per Field

Paste a sample object — typically one representative API response — and each key comes back with its own pattern. A value like "user@example.com" is recognised as an email and gets the email pattern; "2024-01-01" gets an ISO 8601 date pattern; numbers get ^\\d+$ or a decimal pattern; booleans get ^(true|false)$. Recognised formats are email, phone, URL and ISO date.

Strict Mode for Unrecognised Strings

When a string matches none of the known formats, the default output is a permissive length range. Tick Strict mode and you get an escaped exact-value match instead — useful for enum-like fields such as status codes or currency symbols, where the set of valid values is fixed. The generator reads top-level keys, so flatten or run nested objects separately.

Reference Patterns You Can Copy

For field types the generator cannot infer from a single sample — UUID, semantic version, hex colour, Base64, URL-safe slug — the pattern reference table further down this page lists tested, anchored regexes to copy by hand.

Using Regex in JSON Schema

Generated patterns integrate directly into JSON Schema validation via the pattern keyword.

The pattern Keyword

Add "pattern": "your-regex-here" to any string type field in your JSON Schema. The value must match the regex (using ECMA 262 dialect) for the schema validation to pass. The pattern must match the complete string, not just a substring.

Testing Your Patterns

Always test regex patterns against both valid examples (which should match) and invalid examples (which should not) before deploying. Edge cases like empty strings, unicode characters, and boundary values often reveal pattern issues.

Frequently Asked Questions

What is a JSON regex generator?+
A JSON regex generator turns a sample JSON object into a regular expression for each of its fields. Paste one representative object and every key comes back with an anchored ECMA 262 pattern you can drop into the JSON Schema pattern keyword. Recognised value formats include email addresses, phone numbers, URLs and ISO 8601 dates; numbers and booleans get numeric and true/false patterns.
How do I use a regex pattern in JSON Schema?+
Add a 'pattern' property to any string field in your schema. The value is a regular expression string, for example {"type": "string", "pattern": "^\\d{4}-\\d{2}-\\d{2}$"} to require an ISO date. Two rules matter: backslashes must be doubled because the pattern lives inside a JSON string, and the pattern is unanchored by default, so add ^ and $ if you need the whole value to match.
Does JSON itself support regular expressions?+
No. JSON has only six value types - string, number, object, array, boolean and null - and no regex type. A regular expression can be stored in JSON only as a plain string. Matching happens one level up: either in JSON Schema through the pattern, patternProperties and propertyNames keywords, or in your own code after parsing. That is why 'JSON regex' in practice almost always means JSON Schema regex.
Can I apply a regex to JSON keys instead of values?+
Yes, with two JSON Schema keywords the pattern keyword does not cover. patternProperties applies a subschema to every property whose name matches a regex, which is how you validate dynamic keys such as feature flags or locale codes. propertyNames constrains the key names themselves, so {"propertyNames": {"pattern": "^[a-z][a-zA-Z0-9]*$"}} rejects any key that is not camelCase.
Why does my JSON Schema pattern never match?+
Almost always single-escaped backslashes. Written in a JSON file, "\d" is not a regex digit class - it is an invalid JSON escape, and lenient parsers silently reduce it to the literal letter d. You must write "\\d". The second common cause is the opposite problem: an unanchored pattern such as "\\d{4}" matches 'hello1234world' because JSON Schema tests for a substring match, not a full one.
Can the generator create one pattern from several example values?+
No. It reads a single JSON object and produces one pattern per field rather than generalising across a list of sample values. If a string matches none of the known formats you get a permissive length range, or with Strict mode enabled an escaped exact-value match, which suits enum-like fields. Widen the result by hand, or start from the pattern reference table on this page.
Are JSON regex patterns the same as JavaScript regex?+
Effectively yes. JSON Schema specifies the ECMA 262 dialect, the same one JavaScript uses, so patterns move between a schema and JavaScript code unchanged. The caveat is that patterns from PCRE-based languages such as Python, PHP or Perl may not: lookbehind assertions, named groups in \k form, atomic groups and inline flags like (?i) are not portable, and validators differ in what they tolerate.

Ready-to-Use Regex Patterns for Common JSON Fields

Copy these tested regex patterns directly into your JSON Schema pattern keyword. All patterns use ECMA 262 regex dialect, compatible with JSON Schema validators and JavaScript.

Field Type Pattern Example valid value
Email address^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$alice@example.com
UUID v4^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$550e8400-e29b-41d4-a716-446655440000
ISO 8601 date^\d{4}-\d{2}-\d{2}$2026-03-21
ISO 8601 datetime^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+\-]\d{2}:\d{2})$2026-03-21T14:30:00Z
URL (http/https)^https?:\/\/[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-@?^=%&/~\+#])?$https://example.com/path
IPv4 address^((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$192.168.1.1
Hex color code^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$#FF5733
Semantic version^\d+\.\d+\.\d+$1.2.34
Slug (URL-safe)^[a-z0-9]+(?:-[a-z0-9]+)*$my-product-name
Phone (E.164)^\+[1-9]\d{1,14}$+14155552671
Credit card (length only — no Luhn check)^\d{13,19}$4111111111111111
Base64 encoded^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$SGVsbG8gV29ybGQ=

Complete JSON Schema Example with Regex Validation

Here is a real-world JSON Schema that uses pattern to validate multiple fields in a user registration object. Copy and adapt it for your own API.

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "User Registration",
  "type": "object",
  "required": [
    "email",
    "username",
    "birthDate"
  ],
  "properties": {
    "email": {
      "type": "string",
      "pattern": "^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$",
      "description": "Valid email address"
    },
    "username": {
      "type": "string",
      "pattern": "^[a-z0-9_]{3,20}$",
      "description": "3-20 lowercase alphanumeric characters or underscores"
    },
    "birthDate": {
      "type": "string",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}$",
      "description": "ISO 8601 date (YYYY-MM-DD)"
    },
    "website": {
      "type": "string",
      "pattern": "^https?://.+",
      "description": "Optional URL starting with http or https"
    },
    "phoneNumber": {
      "type": "string",
      "pattern": "^\\+[1-9]\\d{1,14}$",
      "description": "E.164 international phone number"
    }
  }
}

Key Rules When Using pattern in JSON Schema

Regex in JSON vs Regex in JSON Schema

JSON has no regular expression type. Its six value types are string, number, object, array, boolean and null, so a regex can only ever live in a JSON document as an ordinary string. Nothing in the JSON specification will match it against anything.

Matching happens one layer up, and there are only two places it can happen:

So a search for “JSON regex” almost always resolves to one of these two, and in practice usually the first.

Matching JSON Keys with Regex, Not Just Values

The pattern keyword only ever looks at a string value. When the key names themselves are dynamic — feature flags, locale codes, tenant IDs, metric names — you need two different keywords.

patternProperties

Applies a subschema to every property whose name matches the regex. Here any key shaped like a locale code must hold a non-empty string, and additionalProperties: false rejects keys that match nothing:

{
  "type": "object",
  "patternProperties": {
    "^[a-z]{2}(-[A-Z]{2})?$": { "type": "string", "minLength": 1 }
  },
  "additionalProperties": false
}

That accepts {"en": "Hello", "en-GB": "Hello"} and rejects {"english": "Hello"}. Note that patternProperties regexes are deliberately unanchored in the same way as pattern, and a key matching two of them must satisfy both subschemas.

propertyNames

Constrains the key names without saying anything about the values — useful for enforcing a naming convention across a whole object:

{
  "type": "object",
  "propertyNames": { "pattern": "^[a-z][a-zA-Z0-9]*$" }
}

Every key is validated as if it were a string against that subschema, so this object rejects snake_case and PascalCase keys while allowing camelCase.

Why a JSON Schema Pattern Silently Fails

Four failure modes account for nearly every “my pattern does not work” report, and none of them raise an error — the validator simply passes data it should have rejected.

Symptom Cause Fix
Pattern matches nothing at allSingle-escaped backslash. In a JSON file "\d" is an invalid escape, and lenient parsers collapse it to the literal letter d.Write "\\d". Double every backslash.
Junk values pass validationUnanchored pattern. JSON Schema tests for a substring, so \\d{4} accepts "hello1234world".Wrap with ^ and $.
Pattern ignored entirelyApplied to a non-string. pattern is a no-op on numbers, booleans and null.Add "type": "string", or use minimum/maximum for numbers.
Works locally, fails in CIPCRE-only syntax. Lookbehind, atomic groups and inline flags such as (?i) are outside ECMA 262.Stay within ECMA 262; spell out character classes instead of using flags.

Once a pattern is in place, run real payloads through the JSON Schema validator — or a whole directory of them through the batch validator — to confirm it rejects what it should.

Explore more tools: All JSON Tools | Validator | Pretty Print | JSON Diff

Related Tools

JSON Benchmark
Benchmark performance
JSON Optimizer
Reduce payload size
Size Analyzer
Find biggest fields
Mock Generator
Generate sample data
JSON Validator
Validate JSON