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.
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));
Thanks for the info!