Submitted by Jim Cooper on Tue, 06/19/2012 - 00:00
Forums

In the following code concerning the commPct field where it is defined numeric as 3,3:

Is it safe to use encodeJsonStr? Will %char, %editw do the same thing for the numeric fields? Or does the encode trap other errors?

commPct is defined as 3,3 so there is no leading zero. For JSON to work, there must be a leading zero. Is %editW the correct method to accomplish this or is there another way?

rowStr =
     '{+
       "first_name":"' + encodeJsonStr(emp.firstName)         + '", +
       "last_name":"'  + encodeJsonStr(emp.lastName)          + '", +
       "hourly_rate":' + encodeJsonStr(%char(emp.hourlyRate)) + ', +
       "comm_pct":'    + encodeJsonStr(%editW(emp.commPct:'0 .   ')) +
     '}';

Best regards,

Jim

Niels Liisberg

Tue, 06/19/2012 - 00:00

Hi Jim,

Why will you use encodeJsonStr on numbers?  … it only escapes special string chars…

What you need, however, is a good old H-card RPG compiler directive with a DECEDIT parameter, to get a zero before the decimal point;

Like :

h DECEDIT('0,') DATEDIT(*YMD.)  COPYRIGHT('System & Method (C), 2012')

Or in USA:

h DECEDIT('0.') DATEDIT(*YMD.)  COPYRIGHT('System & Method (C), 2012')

Best regards,

Niels