Skip to main content

Hi,

I am trying to build a JSON object using UDO functions. How do I format numeric values to display 2 decimal values, by default the output is either an integer or 6 decimals. Below is a snippet of test code and the output generated:

  

      name = "capitalBalance"
      value = 672.500
      OBJNAME = udoHeaderData
      GOSUB BUILD_JSON_OBJECT
      
      name = "amountAvailable"
      value = "617.00"
      OBJNAME = udoHeaderData
      GOSUB BUILD_JSON_OBJECT
      
      name = "arrears"
      value = 77617.00
      OBJNAME = udoHeaderData
      GOSUB BUILD_JSON_OBJECT

      name = "advance"
      value = 0.00
      OBJNAME = udoHeaderData
      GOSUB BUILD_JSON_OBJECT
  
      name = "interestAccrued"
      value = 0.27
      OBJNAME = udoHeaderData
      GOSUB BUILD_JSON_OBJECT

      name = "installmentDue"
      value = "15437.01"
      OBJNAME = udoHeaderData
      GOSUB BUILD_JSON_OBJECT
      
      GOSUB TEST_AND_DISPLAY_OBJ
      RETURN

BUILD_JSON_OBJECT:
      ST = UDOSetProperty(OBJNAME, name, value) ; !GOSUB TEST_AND_DISPLAY_OBJ
      RETURN

TEST_AND_DISPLAY_OBJ:
      IF ST = UDO_SUCCESS THEN
         ST = UDOWrite(udoHandle, UDOFORMAT_JSON, outputString)
         CRT outputString
         CRT
      END ELSE
         S = UDOGetLastError(errorCode, errorMessage)
         CRT "There was an issue: "
         CRT errorCode, errorMessage
      END
      RETURN
****
   END

>>>>>OUTPUT<<<<<<

{
        "message":      {
                "messageId":    "6b9e8c54-4848-4059-9ba4-8ac8389aea8d",
                "messageTimestamp":     "2022-10-28-10.06.06.192843",
                "version":      1,
                "data": {
                        "accountNumber":        226688009,
                        "currency":     "ZAR",
                        "capitalBalance":       672.500000,
                        "amountAvailable":      617,
                        "arrears":      77617,
                        "advance":      0,
                        "interestAccrued":      0.270000,
                        "installmentDue":       15437.010000
                }
        }
}



------------------------------
Rashid Hoosen
Developer
Investec Bank Limited
Sandton ZA
------------------------------

Hi,

I am trying to build a JSON object using UDO functions. How do I format numeric values to display 2 decimal values, by default the output is either an integer or 6 decimals. Below is a snippet of test code and the output generated:

  

      name = "capitalBalance"
      value = 672.500
      OBJNAME = udoHeaderData
      GOSUB BUILD_JSON_OBJECT
      
      name = "amountAvailable"
      value = "617.00"
      OBJNAME = udoHeaderData
      GOSUB BUILD_JSON_OBJECT
      
      name = "arrears"
      value = 77617.00
      OBJNAME = udoHeaderData
      GOSUB BUILD_JSON_OBJECT

      name = "advance"
      value = 0.00
      OBJNAME = udoHeaderData
      GOSUB BUILD_JSON_OBJECT
  
      name = "interestAccrued"
      value = 0.27
      OBJNAME = udoHeaderData
      GOSUB BUILD_JSON_OBJECT

      name = "installmentDue"
      value = "15437.01"
      OBJNAME = udoHeaderData
      GOSUB BUILD_JSON_OBJECT
      
      GOSUB TEST_AND_DISPLAY_OBJ
      RETURN

BUILD_JSON_OBJECT:
      ST = UDOSetProperty(OBJNAME, name, value) ; !GOSUB TEST_AND_DISPLAY_OBJ
      RETURN

TEST_AND_DISPLAY_OBJ:
      IF ST = UDO_SUCCESS THEN
         ST = UDOWrite(udoHandle, UDOFORMAT_JSON, outputString)
         CRT outputString
         CRT
      END ELSE
         S = UDOGetLastError(errorCode, errorMessage)
         CRT "There was an issue: "
         CRT errorCode, errorMessage
      END
      RETURN
****
   END

>>>>>OUTPUT<<<<<<

{
        "message":      {
                "messageId":    "6b9e8c54-4848-4059-9ba4-8ac8389aea8d",
                "messageTimestamp":     "2022-10-28-10.06.06.192843",
                "version":      1,
                "data": {
                        "accountNumber":        226688009,
                        "currency":     "ZAR",
                        "capitalBalance":       672.500000,
                        "amountAvailable":      617,
                        "arrears":      77617,
                        "advance":      0,
                        "interestAccrued":      0.270000,
                        "installmentDue":       15437.010000
                }
        }
}



------------------------------
Rashid Hoosen
Developer
Investec Bank Limited
Sandton ZA
------------------------------

Hi Rashid,

Unfortunately, UDOWrite currently writes any decimal number out to seven decimal places.

Are you able to create a support ticket to request that UDOWrite write decimal numbers out to the desired number of decimal places?  If not, no problem, I can create an internal ticket for this.

The only workaround I can think of is to search/replace the generated json string for decimal numbers that are too long, and truncate them.  Not ideal, I know.

Steven Wingfield



------------------------------
Steven Wingfield
Principle Software Engineer
Rocket Software
Denver, CO, USA
------------------------------

Hi Rashid,

Unfortunately, UDOWrite currently writes any decimal number out to seven decimal places.

Are you able to create a support ticket to request that UDOWrite write decimal numbers out to the desired number of decimal places?  If not, no problem, I can create an internal ticket for this.

The only workaround I can think of is to search/replace the generated json string for decimal numbers that are too long, and truncate them.  Not ideal, I know.

Steven Wingfield



------------------------------
Steven Wingfield
Principle Software Engineer
Rocket Software
Denver, CO, USA
------------------------------

Internally UDO knows what value the property was set to, as one can see by extracting the value with UDOGetProperty. If UDOWrite simply used the actual stored value instead of "formatting" it, all would be well. 



------------------------------
Phillip Potter
V.P. R&D
Data Management Associates Inc DMA
Cincinnati OH US
------------------------------

Internally UDO knows what value the property was set to, as one can see by extracting the value with UDOGetProperty. If UDOWrite simply used the actual stored value instead of "formatting" it, all would be well. 



------------------------------
Phillip Potter
V.P. R&D
Data Management Associates Inc DMA
Cincinnati OH US
------------------------------

UDO only supports a few JSON types: string, object, and array.

UDO does not support number, boolean, and null.

I have been using UDO for quite a while and have our 100% web code interface relying on it.

I re-wrote your code to get a working example with only three items:

* x8UD     $INCLUDE INCLUDE UDO.H                                    ; * x8UD
     $INCLUDE UNIVERSE.INCLUDE UDO.H                                 ; * x8UV
     name = "capitalBalance":@FM:"amountAvailable":@FM:"arrears"
     value = 672.500:@FM:"617.00":@FM:77617.00
     json = ""
     RTN.STATUS = UDOSetOption(UDOOPTION_OUTPUTMODE, UDO_OUTPUT_COMPACT)
     RTN.STATUS = UDOCreate(UDO_OBJECT,udoObj)
     MAX.ROWS = DCOUNT(name,@FM)            
     FOR X = 1 TO MAX.ROWS
        RTN.STATUS = UDOSetProperty(udoObj,name,value)
     NEXT X
     RTN.STATUS = UDOWrite(udoObj, UDOFORMAT_JSON, json)
     RTN.STATUS = UDOFree(udoObj)
     CRT json
     STOP
  END
Posted: 06-30-2023 19:10


------------------------------
Doug Averch
Owner
U2 Logic
www.u2logic.com
------------------------------

Hi Rashid,

Unfortunately, UDOWrite currently writes any decimal number out to seven decimal places.

Are you able to create a support ticket to request that UDOWrite write decimal numbers out to the desired number of decimal places?  If not, no problem, I can create an internal ticket for this.

The only workaround I can think of is to search/replace the generated json string for decimal numbers that are too long, and truncate them.  Not ideal, I know.

Steven Wingfield



------------------------------
Steven Wingfield
Principle Software Engineer
Rocket Software
Denver, CO, USA
------------------------------

Hi Steven,

Thanks for the response. I would appreciate if you could log a support ticket on my behalf.

Rashid



------------------------------
Rashid Hoosen
Developer
Investec Bank Limited
Sandton ZA
------------------------------

Internally UDO knows what value the property was set to, as one can see by extracting the value with UDOGetProperty. If UDOWrite simply used the actual stored value instead of "formatting" it, all would be well. 



------------------------------
Phillip Potter
V.P. R&D
Data Management Associates Inc DMA
Cincinnati OH US
------------------------------

Thanks Phillip.



------------------------------
Rashid Hoosen
Developer
Investec Bank Limited
Sandton ZA
------------------------------

UDO only supports a few JSON types: string, object, and array.

UDO does not support number, boolean, and null.

I have been using UDO for quite a while and have our 100% web code interface relying on it.

I re-wrote your code to get a working example with only three items:

* x8UD     $INCLUDE INCLUDE UDO.H                                    ; * x8UD
     $INCLUDE UNIVERSE.INCLUDE UDO.H                                 ; * x8UV
     name = "capitalBalance":@FM:"amountAvailable":@FM:"arrears"
     value = 672.500:@FM:"617.00":@FM:77617.00
     json = ""
     RTN.STATUS = UDOSetOption(UDOOPTION_OUTPUTMODE, UDO_OUTPUT_COMPACT)
     RTN.STATUS = UDOCreate(UDO_OBJECT,udoObj)
     MAX.ROWS = DCOUNT(name,@FM)            
     FOR X = 1 TO MAX.ROWS
        RTN.STATUS = UDOSetProperty(udoObj,name,value)
     NEXT X
     RTN.STATUS = UDOWrite(udoObj, UDOFORMAT_JSON, json)
     RTN.STATUS = UDOFree(udoObj)
     CRT json
     STOP
  END
Posted: 06-30-2023 19:10


------------------------------
Doug Averch
Owner
U2 Logic
www.u2logic.com
------------------------------

Hi Doug,

The requirement is to return a number, but not in a string.  

Your feedback is appreciated.



------------------------------
Rashid Hoosen
Developer
Investec Bank Limited
Sandton ZA
------------------------------

Hi Steven,

Thanks for the response. I would appreciate if you could log a support ticket on my behalf.

Rashid



------------------------------
Rashid Hoosen
Developer
Investec Bank Limited
Sandton ZA
------------------------------

Hi Rashid, I created two tickets, one for UniVerse (UNV-34256) and one for UniData (UDT-19418).



------------------------------
Steven Wingfield
Principal Software Engineer
Rocket Software
College Station, TX, USA
------------------------------

UDO only supports a few JSON types: string, object, and array.

UDO does not support number, boolean, and null.

I have been using UDO for quite a while and have our 100% web code interface relying on it.

I re-wrote your code to get a working example with only three items:

* x8UD     $INCLUDE INCLUDE UDO.H                                    ; * x8UD
     $INCLUDE UNIVERSE.INCLUDE UDO.H                                 ; * x8UV
     name = "capitalBalance":@FM:"amountAvailable":@FM:"arrears"
     value = 672.500:@FM:"617.00":@FM:77617.00
     json = ""
     RTN.STATUS = UDOSetOption(UDOOPTION_OUTPUTMODE, UDO_OUTPUT_COMPACT)
     RTN.STATUS = UDOCreate(UDO_OBJECT,udoObj)
     MAX.ROWS = DCOUNT(name,@FM)            
     FOR X = 1 TO MAX.ROWS
        RTN.STATUS = UDOSetProperty(udoObj,name,value)
     NEXT X
     RTN.STATUS = UDOWrite(udoObj, UDOFORMAT_JSON, json)
     RTN.STATUS = UDOFree(udoObj)
     CRT json
     STOP
  END
Posted: 06-30-2023 19:10


------------------------------
Doug Averch
Owner
U2 Logic
www.u2logic.com
------------------------------

Doug,

UDO supports numbers, Boolean and Null types.

I created a blog post that describes how to add the different types.

Adding an Array from one JSON Object to another with UDO

While I did cover how to add Boolean and Null types, I did not go into the difference between a string and a number in U2 in great detail.

In U2, a variable can contain a string representation of a number or it  can contain a number, and it is converted to the number when used in a mathematical equation.  If the string is not a number, and you use it in a mathematical function, you will see a 'Non-numeric found when numeric required' run time error.

UDO works off the internal representation of the variable, if it is a string it adds a string, if it is a number it adds a number.

So, if you want to make sure the variable that contains the number is added to the UDOObject as a number, add zero to the variable prior to doing the UDOSetProperty. 

 ( I use the NUM function to check if the string is a number or not prior to doing a += 0 to avoid the runtime error. )  



------------------------------
Mike Rajkowski
MultiValue Product Evangelist
Rocket Internal - All Brands
US
------------------------------


Doug,

UDO supports numbers, Boolean and Null types.

I created a blog post that describes how to add the different types.

Adding an Array from one JSON Object to another with UDO

While I did cover how to add Boolean and Null types, I did not go into the difference between a string and a number in U2 in great detail.

In U2, a variable can contain a string representation of a number or it  can contain a number, and it is converted to the number when used in a mathematical equation.  If the string is not a number, and you use it in a mathematical function, you will see a 'Non-numeric found when numeric required' run time error.

UDO works off the internal representation of the variable, if it is a string it adds a string, if it is a number it adds a number.

So, if you want to make sure the variable that contains the number is added to the UDOObject as a number, add zero to the variable prior to doing the UDOSetProperty. 

 ( I use the NUM function to check if the string is a number or not prior to doing a += 0 to avoid the runtime error. )  



------------------------------
Mike Rajkowski
MultiValue Product Evangelist
Rocket Internal - All Brands
US
------------------------------

Hi Mike,

I did read your post initially and used the code posted there as a basis of my test code.

Thanks.



------------------------------
Rashid Hoosen
Developer
Investec Bank Limited
Sandton ZA
------------------------------

Hi Rashid, I created two tickets, one for UniVerse (UNV-34256) and one for UniData (UDT-19418).



------------------------------
Steven Wingfield
Principal Software Engineer
Rocket Software
College Station, TX, USA
------------------------------

Awesome, thank you Steve.



------------------------------
Rashid Hoosen
Developer
Investec Bank Limited
Sandton ZA
------------------------------