[javascript] What is the convention in JSON for empty vs. null?

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?

This question is related to javascript json

The answer is


Empty array for empty collections and null for everything else.


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.


"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.