There are two numeric types in JSON Schema: integer and number. They share the same validation keywords.
JSON has no standard way to represent complex numbers, so there is no way to test for them in JSON Schema.
integerThe integer
type is used for integral numbers. JSON does not have distinct types for integers and floating-point values. Therefore, the presence or absence of a decimal point is not enough to distinguish between integers and non-integers. For example, 1
and 1.0
are two ways to represent the same value in JSON. JSON Schema considers that value an integer no matter which representation was used.
Language-specific info:
Python
Ruby
Objective-C
Swift
In Python, "integer" is analogous to the int
type.
compliant to schema
compliant to schema
Numbers with a zero fractional part are considered integers:
compliant to schema
Floating point numbers are rejected:
not compliant to schema
Numbers as strings are rejected:
not compliant to schema
numberThe number
type is used for any numeric type, either integers or floating point numbers.
Language-specific info:
Python
Ruby
Objective-C
Swift
In Python, "number" is analogous to the float
type.
compliant to schema
compliant to schema
Simple floating point number:
compliant to schema
Exponential notation also works:
compliant to schema
Numbers as strings are rejected:
not compliant to schema
MultiplesNumbers can be restricted to a multiple of a given number, using the multipleOf
keyword. It may be set to any positive number.
{ "type": "number", "multipleOf" : 10}
compliant to schema
compliant to schema
compliant to schema
not compliant to schema
The multiple can be a floating point number:
schema{ "type": "number", "multipleOf" : 0.01}
compliant to schema
not compliant to schema
Note
The JSON Specification defines numerical precision independently of the IEEE 754 standard. This means developers do not need to worry about the typical limitations of floating-point arithmetic, such as precision loss or representation errors, which are common in most programming languages.
RangeRanges of numbers are specified using a combination of the minimum
and maximum
keywords, (or exclusiveMinimum
and exclusiveMaximum
for expressing exclusive range).
If x is the value being validated, the following must hold true:
x ≥ minimum
x > exclusiveMinimum
x ≤ maximum
x < exclusiveMaximum
While you can specify both of minimum
and exclusiveMinimum
or both of maximum
and exclusiveMaximum
, it doesn't really make sense to do so.
{ "type": "number", "minimum": 0, "exclusiveMaximum": 100}
Less than minimum
:
not compliant to schema
minimum
is inclusive, so 0 is valid:
compliant to schema
compliant to schema
compliant to schema
exclusiveMaximum
is exclusive, so 100 is not valid:
not compliant to schema
Greater than maximum
:
not compliant to schema
In JSON Schema Draft 4, exclusiveMinimum
and exclusiveMaximum
work differently. There they are boolean values, that indicate whether minimum
and maximum
are exclusive of the value. For example:
if exclusiveMinimum
is false
, x ≥ minimum
if exclusiveMinimum
is true
, x > minimum
.
This was changed to have better keyword independence.
Here is an example using the older Draft 4 convention:
schema{ "type": "number", "minimum": 0, "maximum": 100, "exclusiveMaximum": true}
Less than minimum
:
not compliant to schema
exclusiveMinimum
was not specified, so 0 is included:
compliant to schema
compliant to schema
compliant to schema
exclusiveMaximum
is true
, so 100 is not included:
not compliant to schema
Greater than maximum
:
not compliant to schema
Need Help? Did you find these docs helpful? Help us make our docs great!At JSON Schema, we value docs contributions as much as every other type of contribution!
Still Need Help?Learning JSON Schema is often confusing, but don't worry, we are here to help!.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4