91

I know that in most programming scenarios, the preference is for empty collections to null collections when there are 0 elements. However, most languages that consume JSON (like JavaScript) will treat empty lists/objects as true and null ones as false. For example this would be both true and an object in JavaScript:

{
    "items_in_stock": {"widgets":10, "gadgets": 5}
}

But this is also true:

{
    "items_in_stock": {}
}

And this is false:

{
    "items_in_stock": null
}

Is there a convention on empty objects/lists for JSON? And what about for numbers, booleans, and strings?

Lii
  • 11,553
  • 8
  • 64
  • 88
rzrelyea
  • 1,467
  • 1
  • 13
  • 19
  • 1
    related: [Should JSON include null values](http://stackoverflow.com/q/11003424/1048572) – Bergi Jun 20 '12 at 21:46
  • related question not answered here: how does JSON treat empty strings. And additionally, will a zero-length string for a non-string object be treated as a null? – tdugan Oct 19 '15 at 15:29

4 Answers4

91

It is good programming practice to return an empty array [] if the expected return type is an array. This makes sure that the receiver of the json can treat the value as an array immediately without having to first check for null. It's the same way with empty objects using open-closed braces {}.

Strings, Booleans and integers do not have an 'empty' form, so there it is okay to use null values.

This is also addressed in Joshua Blochs excellent book "Effective Java". There he describes some very good generic programming practices (often applicable to other programming langages as well). Returning empty collections instead of nulls is one of them.

Here's a link to that part of his book:

http://jtechies.blogspot.nl/2012/07/item-43-return-empty-arrays-or.html

Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
Koen Peters
  • 12,798
  • 6
  • 36
  • 59
  • 3
    In JavaScript would you check if (items_in_stock == null) or would you check if (items_in_stock)? Do JavaScript developers prefer not to use the "truthy/falsey" features of the language? – rzrelyea Apr 20 '12 at 16:38
  • 3
    Most people use it a lot. Personally I try to avoid using truthy / falsy code, although it does have its benefits (less code). It can lead to hard to track bugs. For instance: Many people set default values for their function params like this : _function func(a) { a = a || ''; ... }_. When _a_ is undefined, null, 0, -1, '', false or NaN it will get the value '', which can or cannot be what you intended (mostly you want only undefined or null). This can lead to strange behavior. Many people use it though because its convenient. – Koen Peters Apr 21 '12 at 15:16
  • 1
    I don't suppose you have any documentation links to back that up? I'm trying to convince an API developer of this same thing, but it's an uphill battle. – MikeyWard Apr 10 '13 at 15:04
  • Yes, In Joshua Blochs excellent book "Effective Java" he describes some very good generic programming practices. Returning empty collections instead of nulls is one of them. Here's a link to that part of his book: http://jtechies.blogspot.nl/2012/07/item-43-return-empty-arrays-or.html – Koen Peters Jul 23 '13 at 10:25
  • 4
    Empty string "" is allowed: http://json.org/ Clearly the syntax diagram shows the nothing path from leading to closing quote. – Wheezil Oct 03 '14 at 14:35
  • 1
    Without context "use empty array instead of null" is purely opinion-based. – Mariusz Jamro Nov 05 '18 at 14:12
  • 1
    @MariuszJamro, can you give an example where you feel it would make more sense to return null instead of an empty array? Seriously asking as every time I try to imagine such a scenario, I don't see how an empty array fails to reiterate the same thing. – Mark A. Donohoe Mar 23 '20 at 17:53
  • 1
    @MarkA.Donohoe "Absence of evidence is not evidence of absence" can apply to code too. E.g. if fetching from a cache where each entry is an array, a null result (not cached) likely means something different from an empty array result (cached, empty result set). That said, I'm with you that null is rarely a good choice. Optional would likely be better in my example, and there's certainly a boatload of code in the wild that unnecessarily and unfortunately treats empty as a special case and returns null. – Matt Craig Apr 23 '22 at 10:31
  • Agreed, and good point about the caching, but we're specifically talking about JSON here. That said, one could argue that null means 'leave whatever's already there, there.' which would be another good use case for it. That said, I'd actually go a step further...for cases where you *do* want null, not empty, rather than having a property set to null in the JSON, I would just omit it altogether. That equates to the same thing (when configured as such in the parsers) and has the added benefit of simplifying the JSON, and thus the cost to transmit it too. – Mark A. Donohoe Apr 30 '22 at 01:11
12

"JSON has a special value called null which can be set on any type of data including arrays, objects, number and boolean types."

"The JSON empty concept applies for arrays and objects...Data object does not have a concept of empty lists. Hence, no action is taken on the data object for those properties."

Here is my source.

  • 2
    The source you reference shows how to use both nulls and empty objects/arrays. Maybe I'm not reading it correctly but it doesn't seem to suggest one over the other. And it doesn't address how JavaScript consumption of the JSON influences the choice. – rzrelyea Mar 08 '12 at 15:38
  • 3
    What about empty string. we've been seeing issues where JSON treats empty string (zero length string) as a null (empty field). – tdugan Oct 19 '15 at 15:28
6

There is the question whether we want to differentiate between cases:

  1. "phone" : "" = the value is empty

  2. "phone" : null = the value for "phone" was not set yet

If we want differentiate I would use null for this. Otherwise we would need to add a new field like "isAssigned" or so. This is an old Database issue.

Dave
  • 311
  • 3
  • 4
  • 4
    To differentiate between assigned/unassigned values, we can also use the presence/absence of the "phone" field itself when it comes to, for example, a web API response, where sending only the necessary data can be used if the goal is to minimize traffic. The sensibility of this approach depends on the number of such fields in the web API responses. – AlexDEV.pro Dec 09 '21 at 13:51
5

Empty array for empty collections and null for everything else.

Nik
  • 9,063
  • 7
  • 66
  • 81