168

Hopefully this isn't obvious to others because I find the docs at https://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 than json-schema.org? Where can I find a larger selection of examples? I don't want to read a big document/specification to find something that can easily be demonstrated in a 10 line example.

1

4 Answers 4

270

From https://json-schema.org/understanding-json-schema/reference/type.html

The type keyword may either be a string or an array:

  • If it’s a string, it is the name of one of the basic types above.
  • If it is an array, it must be an array of strings, where each string is the name of one of the basic types, and each element is unique. In this case, the JSON snippet is valid if it matches any of the given types

The same page lists also the defined data type names, including string and null.

Try:

"member_region": { "type": ["string", "null"] }
2
  • This is an updated link to the location in the current spec: <json-schema.org/draft/2020-12/…>
    – Demitri
    Commented Mar 27, 2021 at 18:16
  • This does not work when need use patterns or other filters. null variable can not use in regex or specifics filters.
    – e-info128
    Commented Dec 25, 2023 at 5:01
96

Extending on Explosion Pills answer if you go for the array syntax:

"member_region": { "type": [ "string", "null" ] } // this works

because you are stating a type, not an example/value. You shouldn't go for:

 "member_region": { "type": [ "string", null ] } // this throws an exception
2
  • 4
    This is the only solution that works with schemas for Azure Logic Apps.
    – EricksonG
    Commented Aug 5, 2019 at 16:50
  • 6
    This solution is also recognized by Visual Studio Code.
    – ivosh
    Commented Nov 8, 2019 at 17:39
14

anyOf may also be useful.

"member_region": {
  "anyOf": [
    { "type": "string" },
    { "type": "null" },
  ] 
}
-4

["string", "null"] will work if you're not using a RegEx pattern.

I'm using a date field validator:

    (0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\\d\\d 

to allow a MM/dd/yyyy pattern, but it will throw a non-match error at an empty string ("")

2
  • 1
    An empty string is not equivalent to null in JSON. Commented Apr 30, 2021 at 16:54
  • Actually, I fixed this with this: ((0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\\d\\d )*? Commented May 1, 2021 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.