Skip to main content

Resolving Null, Empty String, and Unassigned Values

Resolving Null, Empty String, and Unassigned Values

Although you can assign a JSON null value to an element or property, the value will always be returned as "" (ObjectScript empty string). An empty string will also be returned if you attempt to get the value of an unassigned element. You can use %GetTypeOf() to identify the actual datatype in each case.

This example will test a sparse array containing a JSON null value and an empty string. Although array element 2 has no assigned value, it will be represented in the JSON string by a null:

   set array = [null,""]
   do array.%Set(3,"last")
   write array.%ToJSON()
[null,"",null,"last"]

In most cases you would use %GetNext() to retrieve array values, but this example uses a for loop to return unassigned values that %GetNext() would skip. The index number of the last element is array.%Size()-1, but the loop counter is deliberately set to go past the end of the array:

   for i=0:1:(array.%Size()) {write !,i_". value="""_array.%Get(i)_""" type="_array.%GetTypeOf(i)}
0. value="" type=null
1. value="" type=string
2. value="" type=unassigned
3. value="last" type=string
4. value="" type=unassigned

In this example, %Get() returns an empty string in four different cases:

  1. element 0 is a JSON null value, which %GetTypeOf() identifies as datatype null.

  2. element 1 is an empty string, which is identified as datatype string.

  3. Element 2 has no value, and is identified as datatype unassigned.

  4. Although element 3 is the last one in the array, the example attempts to get a datatype for non-existent element 4, which is also identified as datatype unassigned. Valid array index numbers will always be less than array.%Size().

Note:

The distinction between null and unassigned is ObjectScript metadata that will not be preserved when a dynamic entity is serialized to a JSON string. All unassigned elements will be serialized as null values. See “Understanding Sparse Arrays and Unassigned Values” for details.

FeedbackOpens in a new tab