ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

YAJLINTO trouble.. need help

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

  • YAJLINTO trouble.. need help

    Hi, running a simple test for digesting REST API return data using DATA-INTO and YAJL. I have the following code. I've been at this for 14 hours now so I'm kind of hard stuck.

    I'm getting error "not matching rpg variables "** and I'm not understanding how come

    The json data is wrapped around a {} = Dcl-ds Err_Ds
    Then inside it is object errors = Dcl-ds errors ;
    Inside errors I got code and message = code int(3); message Varchar(999) Dim(999) ;

    I already have allowextra=yes and case=convert, I don't see why it would fail at all

    Code:
    dcl-ds Err_Ds Qualified;
      Dcl-ds errors ;
        code Int(3) ;
        message Varchar(999) Dim(999) ;
      END-DS;
    END-DS;    
    
    myJSON = '{ +
      "errors": { +
        "code": 404, +
        "message": [ +
          "Some kind of error message" +
        ] +
      } +
    } ' ;
    
    data-into Error_Ds %DATA(myJSON:'case=convert allowextra=yes') %PARSER('YAJLINTO');

  • #2
    Sorry to hear that this has caused you to struggle so long! I imagine that's frustrating.

    I do see some problems:
    1. You've declared message with DIM(999) elements, but the document has only 1. That is why you're getting "not matching RPG variables" error. The solution is to use the countprefix option.
    2. You've declared 'code' as int(3), but int(3) only allows values from 0-255, so you won't be able to store a value like 404 in it.
    3. You've coded allowextra=yes, which shouldn't be needed here? Though, perhaps it is, perhaps there are other elements that the sender could send?
    4. Your DS is named ERR_DS, but on data-into you refer to it as ERROR_DS.


    I modified your code (had to add a few things to make it runnable so I could try it) see if this works for you:

    Code:
    **free
    
    dcl-ds Err_Ds Qualified;
      Dcl-ds errors ;
        code Int(10) ;
        num_message int(10);
        message Varchar(999) Dim(999);
      END-DS;
    END-DS;
    
    dcl-s myJSON varchar(1000);
    
    myJSON = '{ +
      "errors": { +
        "code": 404, +
        "message": [ +
          "Some kind of error message" +
        ] +
      } +
    } ' ;
    
    data-into Err_Ds %DATA(myJSON:'case=convert countprefix=num_') %PARSER('YAJLINTO');
    
    *inlr = *on;

    Comment


    • Scott Klement
      Scott Klement commented
      Editing a comment
      Oops, the range of INT(3) is actually -128 to 127. I was thinking of UNS(3), not sure why. But, in either case, you won't be able to fit 404 into it.

  • #3
    Thank you Scott it worked!

    I don't know what on earth was I thinking that INT(3) would accept upto = 999

    Comment


    • #4
      Originally posted by OLDMAN25 View Post
      I don't know what on earth was I thinking that INT(3) would accept upto = 999
      You aren't the first person to make that mistake, and you won't be the last either. One of my colleagues just today is fixing a bug caused by that very same thing.

      Comment


      • #5
        I don't understand this. In what situations will INT(3) be limited to 0 - 255 ? What about INT(4) etc... ?
        Is this an RPG thing ?

        "...2. You've declared 'code' as int(3), but int(3) only allows values from 0-255, so you won't be able to store a value like 404 in it...."
        Last edited by MFisher; August 11, 2020, 11:36 AM.

        Comment


        • #6
          Int(3) is RPG;s way of saying it wants a 1 byte integer. It's range is actually -128 to +127. To hold the range 0 - 255 would require an _unsigned_ integer uns(3).
          there is no such thing in RPG as int(4).

          The next increment is int(5) which is two bytes and holds -32 768 to 32 767 or in unsigned from uns(5) from 0 to 65535.

          There are also int(10) - 4 bytes and int(20) - 8 bytes. Plus their uns equivalents.

          This is a historical thing stemming from RPG normally describing the length of numerics by the number of digits. So an int(5) has a capacity of 5 digits - except of course it is not the _full_ 5 digits (i.e. not all the way to 99,999)>

          Comment

          Working...
          X