Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should JSON include null values [closed]

I'm creating an API that returns results as JSON. Is there a current best practice for whether we should include keys in the result when the value is null? For example:

{     "title":"Foo Bar",     "author":"Joe Blow",     "isbn":null } 

or

{     "title":"Foo Bar",     "author":"Joe Blow" } 

Since the second is smaller I am leaning towards this style, but I'm not sure if there is a preferred style or not. From a client perspective it seems like both styles would be functionally equivalent. Any pros or cons to each?

like image 339
jjathman Avatar asked Jun 12 '12 19:06

jjathman


People also ask

Do not include null values in JSON response?

You can ignore null fields at the class level by using @JsonInclude(Include. NON_NULL) to only include non-null fields, thus excluding any attribute whose value is null. You can also use the same annotation at the field level to instruct Jackson to ignore that field while converting Java object to json if it's null.

How do you handle a null response in JSON?

You can ignore null fields at the class level by using @JsonInclude(Include. NON_NULL) to only include non-null fields, thus excluding any attribute whose value is null. You can also use the same annotation at the field level to instruct Jackson to ignore that field while converting Java object to json if it's null.

Can JSON have empty values?

It can also be a list or a bare value (i.e. string, number, boolean or null). If you want to represent a null value in JSON, the entire JSON string (excluding the quotes containing the JSON string) is simply null .

Is empty string null in JSON?

Yes, JSON has the null value (which is indeed treated as a value, not as the absence of value), and the empty string, and they are different.


2 Answers

I am a fan of always including null explicitly as that carries meaning. While omitting a property leaves ambiguity.

As long as your protocol with the server is agreed upon any of the above can work, but if you pass nulls from the server I believe that makes your APIs more flexible later.

Should also mention that javascript's hasOwnProperty function gives you further insight.

/* if true object DOES contain the property with *some* value */ if( objectFromJSON.hasOwnProperty( "propertyName" ) )  /* if true object DOES contain the property and it has been set to null */ if( jsonObject.propertyName === null )  /* if true object either DOES NOT contain the property    OR    object DOES contain the property and it has been set to undefined */ if( jsonObject.propertyName === undefined ) 
like image 158
rushkeldon Avatar answered Oct 15 '22 01:10

rushkeldon


The second will save a small amount on bandwidth, but if that were a concern you would also use indexed arrays instead of filling the JSON with keys. Clearly, ["Foo Bar","Joe Blow"] is much shorter than what you have now.

In terms of usability, I don't think it makes any difference. In both cases, if(json.isbn) will skip to the else. There is usually no need to distinguish between null (no value) and undefined (no given value).

like image 37
Niet the Dark Absol Avatar answered Oct 14 '22 23:10

Niet the Dark Absol