JSON & Schema
JSON Schema Pattern
The pattern keyword applies a regular expression to a string instance. Test the regex first, then escape it correctly inside JSON and validate representative values.
Published
Cross-tool workflow
Test Regex → Escape for JSON → Validate Schema
Three functional steps close one task; none of them is a promotional detour.
- Test Regex
Step 1 · Real Regex Tester
Test the pattern before the schema
Use the regex tester to inspect matching behavior before JSON escaping is added.
^[A-Z]{2}-\d{4}$Expected result: XY-9876 matches; DEV-2048, dev-2048 and prefix DEV-2048 do not.
- Escape for JSON
^[A-Z]{2}-\d{4}$"pattern": "^[A-Z]{2}-\\d{4}$"The regex contains
\d; the JSON string represents that backslash as\\. - Validate Schema
Step 3 · Real Schema Validator
Validate an anchored identifier
The schema accepts only two uppercase letters, a hyphen and four digits.
{ "type": "string", "pattern": "^[A-Z]{2}-\\d{4}$" }Expected result: XY-9876 is valid; DEV-2048 and dev-2048 are invalid.
Why this fails
Four checks before shipping a pattern
Unanchored patterns can match inside a larger string.
A regex backslash must survive JSON string parsing.
Always test strings that must fail as well.
Keep syntax portable when schemas move between validators.
Practice with the full tool
Validate your production pattern
Take the regex you tested here and validate it inside your real JSON Schema.