Blog Posts Case Management Process Management Process Modeling

NULL Values in JSON

Blog: Collaborative Planning & Social Business

The value known as null and what it means.

Origin

Variables hold a value. In most computer programming languages, and variable is implemented as a space in memory where the value will be found. The name of a variable allows you to refer to it without knowing the actual physical memory address.

If the variable has not been set, many data types offer a value null which means simply that the variable has no value. Either the variable has not been set yet, or the value was removed from the variable. The meaning of null is unambiguous: there is no value here.

JSON is a format for transferring values from system to system. It is text based which makes it independent of particular operating hardware and system software. It depends only on the character set and character encoding into the stream. That makes it extremely portable. It also is defined to support the most common data values: strings, numbers, and booleans.

Here is an example block of JSON:

{
 "name": "Justin Time",
 "home phone": "555-123-4567",
 "mobile phone": "555-404-4321"
}

Options for no value

But what if Justin does not have a home phone? There are two ways to send this in JSON, the first is to simply omit the value, and the second is to send the value as null. Null is a special symbol in JSON, and note that the null is not in quotes so that it is not confused with a string that just happens to have the value “null”.

{
 "name": "Justin Time",
 "mobile phone": "555-404-4321"
}

{
 "name": "Justin Time",
 "home phone": null,
 "mobile phone": "555-404-4321"
}

Most argue that these should be considered EXACTLY the same. JSON is not a programming language which allocates memory for variables, so there is no variable that that needs to be set to not have a value. But versions mean the same thing: there is no value for home phone.

Because the first one is smaller, most people argue that one should prefer the first one. It will transfer and parse a tiny bit faster. There is no additional information in sending the value with null specified. Some will point out that including the null is a way to indicate that a home phone value could have existed, but what a receiving system would do with that is unclear.

Declaring that you will always treat a null value the same as an omitted value will simplify the logic handling them, and more importantly will clarify the meaning to the users so that they make fewer mistakes. The caller does not have to worry about what the difference means because there is no difference.

Empty String

With strings (things surrounded by quotes) there is another option and that is to use an empty string, which is the quotes with nothing between. This would look like this:

{
 ”name”: “Justin Time”,
 ”home phone”: “”,
 ”mobile phone”: “555-404-4321”
}

In a strict sense this is not saying that there is no value here. There is a value, but it is a string without any characters. If you think about a phone number, it still means that there is no phone number. 

Some will argue that an empty string should be considered exactly equivalent with a null. All of the systems I have built have always treated an empty string as exactly equal to a null because I have never found need to treat these values differently. Again, the code in a program does not need to worry about what the difference means because (if everything is coded consistently) there is no difference.

For the Recalcitrant

Some will still claim that null is a different value than omitted. If you wish to implement a system this way, then you must also very clearly define the meaning of this difference. If I set a home phone to null, how is that handled differently than leaving the home phone out of the message? for every use of that data, how is the null phone number going to be treated differently from the omitted phone number.

I am not saying that such reasons to have a different meaning don’t exist, but I have designed a lot of systems, and I have never seen the need to consider an omitted value as anything other than equivalent to receiving a null. When translating to Java, if you have a data member (variable), and there is no value in the JSON, it will have to be left as null in Java, because Java allocates space for variables and that memory has to have something in it. 

However if you use a Map in Java to receive the JSON, then it is possible to make a map member with the value of null, and that is different from not having the map member. You can iterate the members of a map, and get a null value. In my Java code I have to be careful to check the value and ignore it if it is null, so that null and omitted values are treated the same. In other languages I have used maps where setting a member to null simply removed the member, thereby enforcing an exact equivalence.

It is possible thought that you have a good reason to considering a null value to be distinct from an omitted value, but if so it is imperative that you specify clearly how exactly each of these values differ in meaning and how they should be handled differently.

Leave a Comment

Get the BPI Web Feed

Using the HTML code below, you can display this Business Process Incubator page content with the current filter and sorting inside your web site for FREE.

Copy/Paste this code in your website html code:

<iframe src="https://www.businessprocessincubator.com/content/null-values-in-json/?feed=html" frameborder="0" scrolling="auto" width="100%" height="700">

Customizing your BPI Web Feed

You can click on the Get the BPI Web Feed link on any of our page to create the best possible feed for your site. Here are a few tips to customize your BPI Web Feed.

Customizing the Content Filter
On any page, you can add filter criteria using the MORE FILTERS interface:

Customizing the Content Filter

Customizing the Content Sorting
Clicking on the sorting options will also change the way your BPI Web Feed will be ordered on your site:

Get the BPI Web Feed

Some integration examples

BPMN.org

XPDL.org

×