JSON string field does not have the double quotes
I have a json object coming in on a service with a field that has "NaN" as its value.
"ReferralInOutOfNetworkCd": "NaN",
In my DTL, I do a straight copy. yet what goes outbound is NaN, without being wrapped in double quotes
"consulting_provider_network_ordering_rd":NaN,
Has anyone experience this? If so, did you find a way to fix or work around it?
Comments
The issue originates from how JSON handles numeric and special values like “NaN”. These values are not enclosed in double quotes in JSON serialization. Here are key insights and a possible resolution:
-
JSON Special Values:
- JSON handles specific special values such as
null,true,false, andNaNwithout enclosing them in double quotes. These values are treated natively and stored as lowercase in JSON structures. Attempting to force double quotes aroundNaNor similar special values may require additional processing 1.
- JSON handles specific special values such as
-
Serialization Approach:
- Some serialization methods (like
%ZEN.Auxiliary.jsonProvider) have parameters to control how values are serialized. In older methods, numeric values might always be treated as numbers regardless of their original property type unless additional adjustments are made to override this behavior 2.
- Some serialization methods (like
-
Custom Handling Approach:
-
You can manage this serialization issue by explicitly treating properties like strings in your custom classes. This ensures that numbers or special values are always serialized enclosed in double quotes. For example:
Class dc.Test Extends %RegisteredObject { Property Value As %String; }When outputting the JSON stream:
set object = ##class(dc.Test).%New() set object.Value = "NaN" set stream = ##class(%ZEN.Auxiliary.jsonProvider).%WriteJSONStreamFromObject(.json, object)
-
This ensures proper quoting for numeric or string values while maintaining standard JSON behavior for “NaN” and similar special values 2.
Let me know if you need a deeper explanation or examples to refine your handling further!
Sources:
First, there is no NaN in JSON.
set dynObj = {"val":($double("NAN"))}
write dynObj.val + 123 ---> "NAN
zwrite dynObj
dynObj={"val":($double("NAN"))} ; <DYNAMIC OBJECT>
/* Question @devcommunity manager: how to exit the "insert code block" function? */
Second, red this for more understanding
the new Editor for DC is a nightmare !
The %ToJSON() method of the %DynamicAbstractObject classes in the ObjectScript languageshould never generate NaN without quotation marks. Although a %DynamicAbstractObject subclass can contain IEEE double-precision floating-point values including the special values Infinity and NaN, using %ToJSON on a %DynamicObject or a %DynamicArray that contains such special values will result in an <ILLEGAL VALUE> error signal since the JSON standard does not support such special IEEE floating-point values. It is possible some applications have extended the JSON standard to include NaN and Infinity as numeric values without quotation marks, the ObjectScript %ToJSON method does not support such an extension. However, @Julius Kavay did give an example how the ObjectScript ZWRITE command will display a %DynamicObject entry containing an IEEE NaN value.