Structured data explained
How Do patternProperties Work in JSON Schema? A Tested Example
Use a small Draft 2020-12 fixture to see how regexes select object keys, how overlapping rules combine, and how to catch a missing type check.

At a glance
A pattern selects property names, and its schema checks their values. If two patterns match the same name, the value must pass both.
In this guide
Say a report can include any number of named counters. You know the names will start with count_, but you cannot list every report field ahead of time. JSON Schema’s patternProperties lets a regular expression select the names and a subschema check the corresponding values. The distinction matters when a key matches two patterns or when a numeric limit seems to let text through.
Match the property name
With the expression ^count_[a-z]+$, the caret starts at the beginning of the name. The final dollar sign closes the match at the end. The part between them asks for count_ followed by one or more lowercase letters. A name such as count_views fits. The names my_count_views and count_views_today do not fit this particular pattern.
The regex inspects the name. Once it matches, the attached schema inspects the value. In this example the attached schema, {"type":"integer","minimum":0}, accepts 125 at count_views, while rejecting the string "125". The string has familiar digits, but JSON still gives it a different type.

Name and value are separate checks
Name match
The regex can recognize count_views. It never sees the value 125.
Value check
After a name matches, its schema can insist that the value is an integer at least zero.
Check the anchoring examples in the Object reference: pattern properties.
Sources: JSON Schema
Build a small fixture
Start with an object and one ordinary field named site. Define its string type in properties and include it in required. Give patternProperties the count-prefix rule above and a second rule, "^.*_errors$": {"maximum":100}. Then add "additionalProperties":false to isolate unexpected names. Each snippet belongs at the appropriate schema level. Declare Draft 2020-12 for the full example.
An instance with site and no count keys passes this minimal schema. The family rule constrains fields that appear. It does not require a name from that family. For a readable first test, set site to "journal" and add "count_views":125. Try the same object with counter_views instead.

Sources: Object reference: pattern properties · Draft 2020-12 Core: patternProperties
Read overlapping rules
The name count_errors matches the prefix and the suffix. A value of 100 passes both. A value of 101 passes the nonnegative counter rule and fails the error ceiling. A value of minus one fails the minimum even though it falls below the ceiling. There is no priority order between matching patterns.
This is also why changing additionalProperties will not fix a failed match. That keyword handles names left over after the exact-name and pattern rules are considered in the same schema. A string assigned to count_views is already in a recognized property. Its failure is about the value it carries.
| Key and value | Matching rule | Result |
|---|---|---|
| count_views = 125 | Count prefix | Pass |
| count_views = "125" | Count prefix | Fail: type |
| count_errors = 101 | Count prefix and error suffix | Fail: maximum |
| counter_views = 7 | Neither pattern | Fail: additionalProperties |

Sources: JSON Schema
Test the type as well as its range
Now try service_errors with a value of "many". This name matches the suffix rule alone. In this small schema, the string passes. A maximum on its own constrains numbers. The worked example was checked with Draft 2020-12 in Ajv with strict-type warnings disabled for the intentionally incomplete rule. A strict linter or validator configuration may warn or reject the schema earlier.
Put the explicit type beside the ceiling to make the intention clear. Use {"type":"integer","maximum":100} for the suffix. Then rerun service_errors. The string fails the repaired type check. The count_errors row with 101 still fails the ceiling, and the boundary case with 100 still passes. Repairing one branch need not change what the other branch already does correctly.

Compare the scope of the numeric assertion in Draft 2020-12 Validation: maximum.
Sources: JSON Schema
Keep examples with the schema
Keep the original and corrected cases in your test suite. Record the exact dialect and which keyword produced each failure. A broken name, an unexpected value type and a value outside its range tell you to look in three different places. Include a case with only the required fixed field so a future schema change cannot silently turn the optional counter family into a requirement.
Check the four cases that reveal gaps
- Try a known key with a good value.
- Try a key that nearly matches the prefix.
- Try a key that matches two patterns.
- Try a string against the suffix rule before and after adding its type.
For the related task, see Read about additionalProperties.
Sources: Object reference: pattern properties · Draft 2020-12 Core: patternProperties
Frequently asked questions
Does patternProperties make a matching field required?
No. It checks matching fields when present. In the example, only the explicitly required site key must be present.
Why does a prefix regex need a leading caret?
Without a start anchor, a match can occur later in the name. A trailing end anchor also prevents accepting unexpected suffixes.
What if properties names an exact key that a regex also matches?
The explicit properties schema and matching pattern schema both apply to that value.
Can I use a JSON-LD validator for this fixture?
Use a validator for the stated Draft 2020-12 JSON Schema dialect. JSON-LD markup checks address a different job.
Validate the JSON Schema
Check that the schema is valid for your chosen dialect.
Check the JSON SchemaContinue with a related guide
- Validate the JSON Schema
Check that the schema is valid for your chosen dialect.
- Read about additionalProperties
Decide how to handle keys that no rule claimed.
- Write a small JSON Schema
Start with a fixed field and a minimal instance.
References
- Object reference: pattern properties JSON Schema
- Draft 2020-12 Core: patternProperties JSON Schema
- Draft 2020-12 Validation: maximum JSON Schema