specify a value can be a string or null with json schema

evanmcdonnal picture evanmcdonnal · Apr 26, 2013 · Viewed 42.6k times · Source

Hopefully this isn't obvious to others because I find the docs at http://json-schema.org/ to be lacking in finer details. I'm getting a block of json with some properties that can be null or a string. How do you specify, in a json schema (to be parsed by json.NET's JsonSchema.Parse method), that a value can be of type null or type string?

Is there something simple I'm missing like supplying an array for the type? For example;

  "member_region": { "type": [ "string", null ] } // this throws an exception

Also, does anyone have a better source for json schema details then the json-schema.org? Where can I find a larger selection of examples? I don't want to read a big doc/spec to find something that can easily be demonstrated in a 10 line example.

Answer

Explosion Pills picture Explosion Pills · Apr 26, 2013

From http://json-schema.org/latest/json-schema-validation.html#anchor79

The value of this keyword MUST be either a string or an array. If it is an array, elements of the array MUST be strings and MUST be unique.

String values MUST be one of the seven primitive types defined by the core specification.

Then we refer to types: http://json-schema.org/latest/json-schema-core.html#anchor8

It lists string and null. Try:

"member_region": { "type": ["string", "null"] }