json schema for null strings, integers, enums, objects, arrays and empty arrays

38,660 views
Skip to first unread message

Rouble

unread,
Sep 12, 2013, 1:39:03 PM9/12/13
to json-...@googlegroups.com
json schema gurus,

When I generate the schema for a string object field I do this (Option A):
    "fooString" : {
        "type" : "string"
    }

This is consistent with schema generated by Jackson/JAXB. One of the consumers of my service is complaining that null is not a valid value for this field. It breaks his python json deserializer.
He suggests (Option B):
    "fooString" : {
        "type": ["string", "null"]
    }

Personally, to me, it is implied that any non-primitive data type can be null. It seems like in the json schema world this needs to be explicitly stated. This makes the schema verbose and, to me, redundant.

Here are some references where this format is recommended: 
http://stackoverflow.com/questions/16241333/specify-a-value-can-be-a-string-or-null-with-json-schema

https://groups.google.com/forum/#!topic/jsonschema/mD6GDca4zN8

Now my questions:

a) Is the Option A schema wrong? Should I inform the Jackson team? Or is this a parsing issue in my client's third party python json library?

b) Does it follow that for all other data types that can be null their schemas would need to be:
    "fooInteger" : {
        "type": ["integer", "null"]
    }

    "fooObject" : {
        "type": ["object", "null"],
        "properties" : { BLAH }
    }

    "fooArray" : {
        {"type": ["array", "null"]},
        "items" : { BLAH }
    }

    "fooEnum" : {
        "type" : [ "string", "null" ],
         "enum" : [ BLAH ]
    }

c) For completeness, let me also ask if an empty list needs any special treatment? Though I think not.

tia,
rouble

Geraint

unread,
Sep 13, 2013, 3:58:51 AM9/13/13
to json-...@googlegroups.com
On Thursday, September 12, 2013 6:39:03 PM UTC+1, Rouble wrote:
json schema gurus,

When I generate the schema for a string object field I do this (Option A):
    "fooString" : {
        "type" : "string"
    }

This is consistent with schema generated by Jackson/JAXB. One of the consumers of my service is complaining that null is not a valid value for this field. It breaks his python json deserializer.
He suggests (Option B):
    "fooString" : {
        "type": ["string", "null"]
    }

Personally, to me, it is implied that any non-primitive data type can be null. It seems like in the json schema world this needs to be explicitly stated. This makes the schema verbose and, to me, redundant.

Here are some references where this format is recommended: 
http://stackoverflow.com/questions/16241333/specify-a-value-can-be-a-string-or-null-with-json-schema

https://groups.google.com/forum/#!topic/jsonschema/mD6GDca4zN8

Now my questions:

a) Is the Option A schema wrong? Should I inform the Jackson team? Or is this a parsing issue in my client's third party python json library?

JSON Schema does not allow null as a value unless it is specifically allowed.  In your first example schema, a null value would not be valid.
 
b) Does it follow that for all other data types that can be null their schemas would need to be:
    "fooInteger" : {
        "type": ["integer", "null"]
    }

    "fooObject" : {
        "type": ["object", "null"],
        "properties" : { BLAH }
    }

    "fooArray" : {
        {"type": ["array", "null"]},
        "items" : { BLAH }
    }

    "fooEnum" : {
        "type" : [ "string", "null" ],
         "enum" : [ BLAH ]
    }
 
If you wish to allow null values for those data types, then you need to explicitly allow null as a type.
 
c) For completeness, let me also ask if an empty list needs any special treatment? Though I think not.

An empty array is still an array (and is different from null, obviously).  All arrays are allowed to be empty unless another constraint (like "minItems") is used.
 
tia,
rouble

Geraint

unread,
Sep 13, 2013, 4:07:24 AM9/13/13
to json-...@googlegroups.com
OK, here is what I think would be the most elegant way to handle this.  Other people may/will disagree:

The issue here is that there are two competing patterns for specifying "no value".  In Java, any class-defined properties have to exist on all instances of that class.  If you want to specify that a string field has no value, then you specify NULL.  I see this as basically a knock-on effect from the world of C, where strings are pointers, but they can point to the special value NULL == 0.

In JSON, however, properties are allowed to simply not exist.  I strongly believe that that the inclusion of null in the JSON format is mostly to make it more friendly to C/Java/etc. developers.

So, what I would personally like to see is for serialisers to produce the schema from Option A - and then if that string value is NULL in Java, to not include it in the output, instead of including it as null.  This produces neat schemas and neat JSON.  It does, however, mandate that de-serialisers must be able to cope with missing properties, treating them as NULL.

Anyway, that's just my opinion, and you don't have to pay any attention to it. :)


On Thursday, September 12, 2013 6:39:03 PM UTC+1, Rouble wrote:

tvrtko.s...@gmail.com

unread,
Mar 5, 2015, 10:25:47 AM3/5/15
to json-...@googlegroups.com
Consider when you get a request to update some data in database:

* If you get posted data like: {"name": "John"}, the user is only changing the name attribute, other unmentioned fields are left unchanged.
* If you get posted data like: {"name": "John", "age": null}, you are supposed to set the age=NULL inside database (clear the field).

This has symmetry with UPDATE statement: if you ommit columns from update, they are not changed.

This object can have many properties, and omitting those properties leaves them unchanged.

I preffer this mode of operation because if you have old client which is not sending some new fileds, those fields won't be cleared by accident, but rather, they will be left unchanged.

Tvrtko

azw...@gmail.com

unread,
Mar 10, 2017, 4:20:40 AM3/10/17
to JSON Schema
i wrote schema like this :

      "ID_NUMBER": {
      "type": "null"}
but got error unsupported field schema for type null...what should i do?

Henry Andrews

unread,
Mar 10, 2017, 12:53:17 PM3/10/17
to json-...@googlegroups.com
So your JSON document consists of an object with one field, "ID_NUMBER"?  Since that is not a recognized JSON Schema keyword, any implementation will ignore its value which is probably confusing the validator.  What implementation are you using?  What is an example instance that the schema should validate?

thanks,
-henry




From: "azw...@gmail.com" <azw...@gmail.com>
To: JSON Schema <json-...@googlegroups.com>
Sent: Thursday, March 9, 2017 11:11 PM
Subject: [json-schema] Re: json schema for null strings, integers, enums, objects, arrays and empty arrays

--
You received this message because you are subscribed to the Google Groups "JSON Schema" group.
To unsubscribe from this group and stop receiving emails from it, send an email to json-schema...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Reply all
Reply to author
Forward
0 new messages