ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Empty array elements returned in JSON via IWS

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Empty array elements returned in JSON via IWS

    So I was trying to see how IWS formats the JSON that is output from an RPGLE SRVPGM. I created a very simple program and while I get the proper results, I also get empty elements output into the JSON. I want to exclude these extra elements from being output. Is this possible with IWS / RPGLE?

    Prototype:
    PHP Code:
    D H_OK c const(200)
    D H_CREATED c const(201)
    D H_NOCONTENT c const(204)
    D H_BADREQUEST c const(400)
    D H_NOTFOUND c const(404)
    D H_CONFLICT c const(409)
    D H_GONE c const(410)
    D H_SERVERERROR c const(500)

    D ERR_DUPLICATE_WRITE...
    D C const(01021)


    DCL-DS families QUALIFIED template DIM(10);    <<<< This Array is returned and no extra elements returned
          name VARCHAR
    (25);
          
    num_children INT(10);
           
    DCL-DS children DIM(5);             <<<<  This is the array(in the above array) I mentioned having the blank elements returned
               name VARCHAR
    (25);
               
    age INT(10);
          
    END-DS;
    END-DS;


    D getAll PR
    D fam_
    ...
    D LENGTH 10i 0
    D fams likeds
    (familiesdim(1000)
    D options(*varsize)
    D httpStatus 10i 0
    D httpHeaders 100a dim
    (10
    Pgm:
    PHP Code:
    h NOMAIN PGMINFO(*PCML:*MODULE:*V7 :*DCLCASE)
    /
    copy tstfamspr


    //************************************************** *************
    // getAll *
    //************************************************** *************
    P getAll B EXPORT
    D getAll PI
    D fam_
    ...
    D LENGTH 10i 0
    D fams likeds
    (familiesdim(1000)
    D options(*varsize)
    D httpStatus 10i 0
    D httpHeaders 100a dim
    (10)
    /
    FREE
    clear httpHeaders
    ;
    clear fams;
    // build the output that will become JSON.

    fam_LENGTH 0;
    fam_LENGTH fam_LENGTH +1;
    fams(fam_LENGTH).name 'test1';
    fams(fam_LENGTH).num_children 2;
    fams(fam_LENGTH).children(1).name 'Bo';
    fams(fam_LENGTH).children(1).age 12;
    fams(fam_LENGTH).children(2).name 'Sam';
    fams(fam_LENGTH).children(2).age 9;
    fam_LENGTH fam_LENGTH +1;
    fams(fam_LENGTH).name 'test2';
    fams(fam_LENGTH).num_children 1;
    fams(fam_LENGTH).children(1).name 'Jo';
    fams(fam_LENGTH).children(1).age 8;



    httpStatus H_OK;
    httpHeaders(1) = 'Cache-Control: no-cache, no-store';

    /
    END-FREE
    P getAll E 
    Result:
    PHP Code:
    {
    "fam_LENGTH"2,
    "fams": [
    {
    "name""test1",
    "num_children"2,
    "children": [
    {
    "name""Bo",
    "age"12
    },
    {
    "name""Sam",
    "age"9
    },
    {
    "name""",
    "age"0
    },
    {
    "name""",
    "age"0
    },
    {
    "name""",
    "age"0
    }
    ]
    },
    {
    "name""test2",
    "num_children"1,
    "children": [
    {
    "name""Jo",
    "age"8
    },
    {
    "name""",
    "age"0                           <<< blank elements returned for this array
    },
    {
    "name""",
    "age"0
    },
    {
    "name""",
    "age"0
    },
    {
    "name""",
    "age"0
    }
    ]
    }
    ],
    "httpStatus"200,
    "httpHeaders": [
    "Cache-Control: no-cache, no-store",
    "",
    "",
    "",
    "",
    "",
    "",
    "",
    "",
    ""
    ]


  • #2
    Yes, you can. I thought I had written an article on the topic but danged if I can find it.

    There are two methods available. Take a look at this proto:
    Code:
    dcl-pi RestSrv4 ExtPgm;
       requestedCat Like(catcod);
       result char(20);
       productList_count int(5);  [B] <<< Count of array elements as separate parm - your choice of name.[/B]
       productList LikeDS(product) Dim(20);
    end-pi;
    When you want to use this approach you must deselect the "Detect length fields" check box on the parameter page. Once you have done this you can select the field productList_count aa the controlling value for the productList parameter (it will have defaulted to a fixed value of 20 - i.e. the Dim value). Using this version will include the count in the JSON.

    The other option is to supply the length in a specially named field within the DS -like this:
    Code:
    dcl-pi RestSrv4 ExtPgm;
       requestedCat Like(catcod);
       result char(20);
       productList LikeDS(productList_T);
    end-pi;
    ​
    Dcl-ds productList_T Qualified Template;
       products_LENGTH int(5);   [B]<<< NOTE "LENGTH" must be in caps![/B]
       products LikeDS(product) Dim(20);
    End-Ds;


    With this version leave the "detect" check box selected and IWS will locate the length field and use it to control the output but will NOT output the value.

    Hope this helps.

    Comment


    • #3
      P.S. I hope you know that you don't need that /Free stuff ny more.

      Comment

      Working...
      X