SET TERM ^ ;

-- FN_TO_JSON para registros de até 100 campos
create or alter function FN_TO_JSON_V3 (
    V00 blob sub_type 1 segment size 80 = '',
    V01 blob sub_type 1 segment size 80 = '',
    V02 blob sub_type 1 segment size 80 = '',
    V03 blob sub_type 1 segment size 80 = '',
    V04 blob sub_type 1 segment size 80 = '',
    V05 blob sub_type 1 segment size 80 = '',
    V06 blob sub_type 1 segment size 80 = '',
    V07 blob sub_type 1 segment size 80 = '',
    V08 blob sub_type 1 segment size 80 = '',
    V09 blob sub_type 1 segment size 80 = '',
    V10 blob sub_type 1 segment size 80 = '',
    V11 blob sub_type 1 segment size 80 = '',
    V12 blob sub_type 1 segment size 80 = '',
    V13 blob sub_type 1 segment size 80 = '',
    V14 blob sub_type 1 segment size 80 = '',
    V15 blob sub_type 1 segment size 80 = '',
    V16 blob sub_type 1 segment size 80 = '',
    V17 blob sub_type 1 segment size 80 = '',
    V18 blob sub_type 1 segment size 80 = '',
    V19 blob sub_type 1 segment size 80 = '',
    V20 blob sub_type 1 segment size 80 = '',
    V21 blob sub_type 1 segment size 80 = '',
    V22 blob sub_type 1 segment size 80 = '',
    V23 blob sub_type 1 segment size 80 = '',
    V24 blob sub_type 1 segment size 80 = '',
    V25 blob sub_type 1 segment size 80 = '',
    V26 blob sub_type 1 segment size 80 = '',
    V27 blob sub_type 1 segment size 80 = '',
    V28 blob sub_type 1 segment size 80 = '',
    V29 blob sub_type 1 segment size 80 = '',
    V30 blob sub_type 1 segment size 80 = '',
    V31 blob sub_type 1 segment size 80 = '',
    V32 blob sub_type 1 segment size 80 = '',
    V33 blob sub_type 1 segment size 80 = '',
    V34 blob sub_type 1 segment size 80 = '',
    V35 blob sub_type 1 segment size 80 = '',
    V36 blob sub_type 1 segment size 80 = '',
    V37 blob sub_type 1 segment size 80 = '',
    V38 blob sub_type 1 segment size 80 = '',
    V39 blob sub_type 1 segment size 80 = '',
    V40 blob sub_type 1 segment size 80 = '',
    V41 blob sub_type 1 segment size 80 = '',
    V42 blob sub_type 1 segment size 80 = '',
    V43 blob sub_type 1 segment size 80 = '',
    V44 blob sub_type 1 segment size 80 = '',
    V45 blob sub_type 1 segment size 80 = '',
    V46 blob sub_type 1 segment size 80 = '',
    V47 blob sub_type 1 segment size 80 = '',
    V48 blob sub_type 1 segment size 80 = '',
    V49 blob sub_type 1 segment size 80 = '',
    V50 blob sub_type 1 segment size 80 = '',
    V51 blob sub_type 1 segment size 80 = '',
    V52 blob sub_type 1 segment size 80 = '',
    V53 blob sub_type 1 segment size 80 = '',
    V54 blob sub_type 1 segment size 80 = '',
    V55 blob sub_type 1 segment size 80 = '',
    V56 blob sub_type 1 segment size 80 = '',
    V57 blob sub_type 1 segment size 80 = '',
    V58 blob sub_type 1 segment size 80 = '',
    V59 blob sub_type 1 segment size 80 = '',
    V60 blob sub_type 1 segment size 80 = '',
    V61 blob sub_type 1 segment size 80 = '',
    V62 blob sub_type 1 segment size 80 = '',
    V63 blob sub_type 1 segment size 80 = '',
    V64 blob sub_type 1 segment size 80 = '',
    V65 blob sub_type 1 segment size 80 = '',
    V66 blob sub_type 1 segment size 80 = '',
    V67 blob sub_type 1 segment size 80 = '',
    V68 blob sub_type 1 segment size 80 = '',
    V69 blob sub_type 1 segment size 80 = '',
    V70 blob sub_type 1 segment size 80 = '',
    V71 blob sub_type 1 segment size 80 = '',
    V72 blob sub_type 1 segment size 80 = '',
    V73 blob sub_type 1 segment size 80 = '',
    V74 blob sub_type 1 segment size 80 = '',
    V75 blob sub_type 1 segment size 80 = '',
    V76 blob sub_type 1 segment size 80 = '',
    V77 blob sub_type 1 segment size 80 = '',
    V78 blob sub_type 1 segment size 80 = '',
    V79 blob sub_type 1 segment size 80 = '',
    V80 blob sub_type 1 segment size 80 = '',
    V81 blob sub_type 1 segment size 80 = '',
    V82 blob sub_type 1 segment size 80 = '',
    V83 blob sub_type 1 segment size 80 = '',
    V84 blob sub_type 1 segment size 80 = '',
    V85 blob sub_type 1 segment size 80 = '',
    V86 blob sub_type 1 segment size 80 = '',
    V87 blob sub_type 1 segment size 80 = '',
    V88 blob sub_type 1 segment size 80 = '',
    V89 blob sub_type 1 segment size 80 = '',
    V90 blob sub_type 1 segment size 80 = '',
    V91 blob sub_type 1 segment size 80 = '',
    V92 blob sub_type 1 segment size 80 = '',
    V93 blob sub_type 1 segment size 80 = '',
    V94 blob sub_type 1 segment size 80 = '',
    V95 blob sub_type 1 segment size 80 = '',
    V96 blob sub_type 1 segment size 80 = '',
    V97 blob sub_type 1 segment size 80 = '',
    V98 blob sub_type 1 segment size 80 = '',
    V99 blob sub_type 1 segment size 80 = '')
returns blob sub_type 1 segment size 80
AS
declare variable json blob sub_type 1 segment size 80;
begin
    JSON = '';

    if(char_length(V00) > 0)then begin JSON = JSON||','||V00; end
    if(char_length(V01) > 0)then begin JSON = JSON||','||V01; end
    if(char_length(V02) > 0)then begin JSON = JSON||','||V02; end
    if(char_length(V03) > 0)then begin JSON = JSON||','||V03; end
    if(char_length(V04) > 0)then begin JSON = JSON||','||V04; end
    if(char_length(V05) > 0)then begin JSON = JSON||','||V05; end
    if(char_length(V06) > 0)then begin JSON = JSON||','||V06; end
    if(char_length(V07) > 0)then begin JSON = JSON||','||V07; end
    if(char_length(V08) > 0)then begin JSON = JSON||','||V08; end
    if(char_length(V09) > 0)then begin JSON = JSON||','||V09; end
    if(char_length(V10) > 0)then begin JSON = JSON||','||V10; end
    if(char_length(V11) > 0)then begin JSON = JSON||','||V11; end
    if(char_length(V12) > 0)then begin JSON = JSON||','||V12; end
    if(char_length(V13) > 0)then begin JSON = JSON||','||V13; end
    if(char_length(V14) > 0)then begin JSON = JSON||','||V14; end
    if(char_length(V15) > 0)then begin JSON = JSON||','||V15; end
    if(char_length(V16) > 0)then begin JSON = JSON||','||V16; end
    if(char_length(V17) > 0)then begin JSON = JSON||','||V17; end
    if(char_length(V18) > 0)then begin JSON = JSON||','||V18; end
    if(char_length(V19) > 0)then begin JSON = JSON||','||V19; end
    if(char_length(V20) > 0)then begin JSON = JSON||','||V20; end
    if(char_length(V21) > 0)then begin JSON = JSON||','||V21; end
    if(char_length(V22) > 0)then begin JSON = JSON||','||V22; end
    if(char_length(V23) > 0)then begin JSON = JSON||','||V23; end
    if(char_length(V24) > 0)then begin JSON = JSON||','||V24; end
    if(char_length(V25) > 0)then begin JSON = JSON||','||V25; end
    if(char_length(V26) > 0)then begin JSON = JSON||','||V26; end
    if(char_length(V27) > 0)then begin JSON = JSON||','||V27; end
    if(char_length(V28) > 0)then begin JSON = JSON||','||V28; end
    if(char_length(V29) > 0)then begin JSON = JSON||','||V29; end
    if(char_length(V30) > 0)then begin JSON = JSON||','||V30; end
    if(char_length(V31) > 0)then begin JSON = JSON||','||V31; end
    if(char_length(V32) > 0)then begin JSON = JSON||','||V32; end
    if(char_length(V33) > 0)then begin JSON = JSON||','||V33; end
    if(char_length(V34) > 0)then begin JSON = JSON||','||V34; end
    if(char_length(V35) > 0)then begin JSON = JSON||','||V35; end
    if(char_length(V36) > 0)then begin JSON = JSON||','||V36; end
    if(char_length(V37) > 0)then begin JSON = JSON||','||V37; end
    if(char_length(V38) > 0)then begin JSON = JSON||','||V38; end
    if(char_length(V39) > 0)then begin JSON = JSON||','||V39; end
    if(char_length(V40) > 0)then begin JSON = JSON||','||V40; end
    if(char_length(V41) > 0)then begin JSON = JSON||','||V41; end
    if(char_length(V42) > 0)then begin JSON = JSON||','||V42; end
    if(char_length(V43) > 0)then begin JSON = JSON||','||V43; end
    if(char_length(V44) > 0)then begin JSON = JSON||','||V44; end
    if(char_length(V45) > 0)then begin JSON = JSON||','||V45; end
    if(char_length(V46) > 0)then begin JSON = JSON||','||V46; end
    if(char_length(V47) > 0)then begin JSON = JSON||','||V47; end
    if(char_length(V48) > 0)then begin JSON = JSON||','||V48; end
    if(char_length(V49) > 0)then begin JSON = JSON||','||V49; end
    if(char_length(V50) > 0)then begin JSON = JSON||','||V50; end
    if(char_length(V51) > 0)then begin JSON = JSON||','||V51; end
    if(char_length(V52) > 0)then begin JSON = JSON||','||V52; end
    if(char_length(V53) > 0)then begin JSON = JSON||','||V53; end
    if(char_length(V54) > 0)then begin JSON = JSON||','||V54; end
    if(char_length(V55) > 0)then begin JSON = JSON||','||V55; end
    if(char_length(V56) > 0)then begin JSON = JSON||','||V56; end
    if(char_length(V57) > 0)then begin JSON = JSON||','||V57; end
    if(char_length(V58) > 0)then begin JSON = JSON||','||V58; end
    if(char_length(V59) > 0)then begin JSON = JSON||','||V59; end
    if(char_length(V60) > 0)then begin JSON = JSON||','||V60; end
    if(char_length(V61) > 0)then begin JSON = JSON||','||V61; end
    if(char_length(V62) > 0)then begin JSON = JSON||','||V62; end
    if(char_length(V63) > 0)then begin JSON = JSON||','||V63; end
    if(char_length(V64) > 0)then begin JSON = JSON||','||V64; end
    if(char_length(V65) > 0)then begin JSON = JSON||','||V65; end
    if(char_length(V66) > 0)then begin JSON = JSON||','||V66; end
    if(char_length(V67) > 0)then begin JSON = JSON||','||V67; end
    if(char_length(V68) > 0)then begin JSON = JSON||','||V68; end
    if(char_length(V69) > 0)then begin JSON = JSON||','||V69; end
    if(char_length(V70) > 0)then begin JSON = JSON||','||V70; end
    if(char_length(V71) > 0)then begin JSON = JSON||','||V71; end
    if(char_length(V72) > 0)then begin JSON = JSON||','||V72; end
    if(char_length(V73) > 0)then begin JSON = JSON||','||V73; end
    if(char_length(V74) > 0)then begin JSON = JSON||','||V74; end
    if(char_length(V75) > 0)then begin JSON = JSON||','||V75; end
    if(char_length(V76) > 0)then begin JSON = JSON||','||V76; end
    if(char_length(V77) > 0)then begin JSON = JSON||','||V77; end
    if(char_length(V78) > 0)then begin JSON = JSON||','||V78; end
    if(char_length(V79) > 0)then begin JSON = JSON||','||V79; end
    if(char_length(V80) > 0)then begin JSON = JSON||','||V80; end
    if(char_length(V81) > 0)then begin JSON = JSON||','||V81; end
    if(char_length(V82) > 0)then begin JSON = JSON||','||V82; end
    if(char_length(V83) > 0)then begin JSON = JSON||','||V83; end
    if(char_length(V84) > 0)then begin JSON = JSON||','||V84; end
    if(char_length(V85) > 0)then begin JSON = JSON||','||V85; end
    if(char_length(V86) > 0)then begin JSON = JSON||','||V86; end
    if(char_length(V87) > 0)then begin JSON = JSON||','||V87; end
    if(char_length(V88) > 0)then begin JSON = JSON||','||V88; end
    if(char_length(V89) > 0)then begin JSON = JSON||','||V89; end
    if(char_length(V90) > 0)then begin JSON = JSON||','||V90; end
    if(char_length(V91) > 0)then begin JSON = JSON||','||V91; end
    if(char_length(V92) > 0)then begin JSON = JSON||','||V92; end
    if(char_length(V93) > 0)then begin JSON = JSON||','||V93; end
    if(char_length(V94) > 0)then begin JSON = JSON||','||V94; end
    if(char_length(V95) > 0)then begin JSON = JSON||','||V95; end
    if(char_length(V96) > 0)then begin JSON = JSON||','||V96; end
    if(char_length(V97) > 0)then begin JSON = JSON||','||V97; end
    if(char_length(V98) > 0)then begin JSON = JSON||','||V98; end
    if(char_length(V99) > 0)then begin JSON = JSON||','||V99; end

 JSON = '{'||JSON||'}';
 JSON = replace(JSON,'{,','{');
 JSON = replace(JSON,',}','}');

 return coalesce(JSON,'{}');
end
^

SET TERM ; ^