Forums >> Programming >> RPG Programming

Jump to:




bvstone

Keeping a Leading Zero Before the Decimal Point with RPG and JSON

Posted:

Keeping a Leading Zero Before the Decimal Point with RPG and JSON

In doing a few JSON projects using YAJL I have run into the same issue a couple times.

JSON, it seems, requires a leading zero before the decimal point in a numeric value.  

For example:

"percent": 0.450

When using the %char Built in Function (BIF) the zero preceding the decimal point is removed.  So, the best option I have discovered is pulling an old trick out of the bag and using the %editw BIF.

Let's say we have a field defined a numeric with 5 digits and 3 decimal places:

D  tax_perc                      5  3 INZ   

If we try to use the following:

yajl_addChar('percent':%char(tax_perc)); 

We will end up with:

"percent": .450

But, if we use:

yajl_addChar('percent':%editw(tax_perc:'0 .   ')); 

Then we end up with the proper format required by JSON:

"percent": 0.450

Where you place the "0" in the edit word will tell the system when to stop zero suppressing the output.  In this case, it will stop right before the decimal.

I'm sure there are more ways to do this, but this is what has worked for me.  


Last edited 07/30/2017 at 15:39:58



Latest Posts:

Why I Cancelled my DynDNS Service and How I Replaced It with an IBM i Application Why I Cancelled my DynDNS Service and How I Replaced It with an IBM i Application
Posted by 8 hours ago
IBM Power Systems >> (QGPL) IBM i
Green Tools for G Suite (G4G) Product Updates (Licensing, Functionality, Base Product) Green Tools for G Suite (G4G) Product Updates (Licensing, Functionality, Base Product)
Posted by July 13, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
Reading JSON Data from Standard Input With YAJL and RPG Reading JSON Data from Standard Input With YAJL and RPG
Posted by July 12, 2019
Programming >> Proof of Concept (POC)
MAILTOOL Updated to Allow Use of IBM Global Security Kit (GSKIT) for SSL/TLS Communications MAILTOOL Updated to Allow Use of IBM Global Security Kit (GSKIT) for SSL/TLS Communications
Posted by June 19, 2019
BVSTools >> BVSTools Announcements >> eMail Tool (MAILTOOL) Specific Announcements
GETURI v10.00 Released Supporting IBM Global Security Kit (GSKIT) and Server Name Indication (SNI) GETURI v10.00 Released Supporting IBM Global Security Kit (GSKIT) and Server Name Indication (SNI)
Posted by June 11, 2019
BVSTools >> BVSTools Announcements >> Get URI (GETURI) Specific Announcements
BVSTools Now Offers Vertex Cloud Interface BVSTools Now Offers Vertex Cloud Interface
Posted by April 15, 2019
BVSTools >> BVSTools Announcements
Token Has an Invalid Signature Error for Office 365 Email Token Has an Invalid Signature Error for Office 365 Email
Posted by March 22, 2019
BVSTools >> BVSTools Software Discussion >> GreenTools for Microsoft Apps (G4MS) Specific Discussion
Resending Emails that have Errored Out with Updated Router or Authentication Information Resending Emails that have Errored Out with Updated Router or Authentication Information
Posted by March 1, 2019
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
BVSTools Offers Toolset to Work With HubSpot OAuth 2.0 APIs On Your IBM i BVSTools Offers Toolset to Work With HubSpot OAuth 2.0 APIs On Your IBM i
Posted by January 27, 2019
BVSTools >> BVSTools Announcements
G4MSDRV Currently Not Supported G4MSDRV Currently Not Supported
Posted by January 17, 2019
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
Removing Trailing Carriage Returns and/or Line Feeds from a String with RPG Removing Trailing Carriage Returns and/or Line Feeds from a String with RPG
Posted by December 26, 2018
Programming >> RPG Programming
Create QRCODE in DDS Create QRCODE in DDS
Posted by September 21, 2018
Programming >> RPG Programming
Base64 Encoding a File with RPG Base64 Encoding a File with RPG
Posted by September 6, 2018
Programming >> RPG Programming
Building JSON with RPG and YAJL and Writing to Standard Output Building JSON with RPG and YAJL and Writing to Standard Output
Posted by August 31, 2018
Programming >> Proof of Concept (POC)
How to Delete Files or Empty Trash From Your Google Drive with your IBM i and RPG/ILE How to Delete Files or Empty Trash From Your Google Drive with your IBM i and RPG/ILE
Posted by July 24, 2018
BVSTools >> BVSTools Software Discussion >> GreenTools for G Suite (Google Apps) (G4G) Specific Discussion
mjhaston

RE: Keeping a Leading Zero Before the Decimal Point with RPG and JSON

Posted:

RE: Keeping a Leading Zero Before the Decimal Point with RPG and JSON

H DECEDIT('0.')   

I have found this H Spec takes care of the issue without additional coding.  Just the usual:

yajl_addNum('taxRate': %char(taxrate));  


Last edited 05/10/2018 at 07:24:12



bvstone

RE: RE: Keeping a Leading Zero Before the Decimal Point with RPG and JSON

Posted:

RE: RE: Keeping a Leading Zero Before the Decimal Point with RPG and JSON

Thanks for the info!


Last edited 05/10/2018 at 07:33:18




Reply




Copyright 1983-2019 BVSTools
GreenBoard(v3) Powered by the eRPG SDK, MAILTOOL Plus!, GreenTools for Google Apps, jQuery, jQuery UI, BlockUI, CKEditor and running on the IBM i (AKA AS/400, iSeries, System i).