Combining Types - Boolean and Null

In out previous examples, we have set the types of properties as string and integer. But what if you want to combine different types for a single property?

Let's add a new property hasAgreedToTerms to the JSON object. This property should be a boolean value, but it can also be null.

1{
2  "name":"John Doe",
3  "age": 25,
4  "hasAgreedToTerms": true,
5}

Boolean

A boolean type can have two values: true or false. you can define a boolean type as follows:

1{
2  "type": "boolean"
3}

Null

The null type has only one value: null. You can define a null type as follows:

1{
2  "type": "null"
3}

Defining the Schema

In JSON Schema, you can define multiple types by passing an array of types to the type field. Example:

1{
2  "type": ["boolean", "null"]
3}

Now, try to modify the hasAgreedToTerms property on the side editor to accept both boolean and null values.

Loading...
Output
Please click the button or use
Shift+Enter
to view the output