To default a value to null, you must specify "default": null
. See the table in the Complex Types section of the specification for examples values of each type.
Does it matter what the data type is?
Yes, the type matters. As you can see in the Primitive Types section of the specification, null
is its own type. You may not set an int
, string
, or any other primitive type to null
.
If you want a "nullable" field, use Unions. For example:
{ "type": ["null", "int"], "default": null }
This dictates that this data may either be an int
or it may be a null
. It defaults to null
. When specifying defaults for unions, remember that the default must match the first type in the union.
From the specification:
(Note that when a default value is specified for a record field whose type is a union, the type of the default value must match the first element of the union. Thus, for unions containing "null", the "null" is usually listed first, since the default value of such unions is typically null.)