ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

YAJLINTO error ; Reason Code 14

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

  • YAJLINTO error ; Reason Code 14

    Hello,

    I have a JSON message where a variable could alternatively contains a value or a Json string itself.


    This is the RPGLE Data structure to MAP json data.


    Click image for larger version

Name:	image.png
Views:	58
Size:	62.2 KB
ID:	159020

    In the example below everything works fine because "name" and "value" are properly mapped into the RPGLE data structure

    Click image for larger version

Name:	image.png
Views:	55
Size:	56.3 KB
ID:	159019

    In this example below "value" contains a Json string itself named "daterange".

    There ia an extra level and I would like to parse "value" as a standard rpgle variable.
    I will then scan "value" variable for "{daterange:" value and from there extract "endDate" and "startDate" via a new RPGLE structure.



    Click image for larger version

Name:	image.png
Views:	56
Size:	97.6 KB
ID:	159018

    I would like to know if this is possibile via YAJLINTO parser.

    Thank you,

    Giovanni

  • #2
    Hi HMiller,

    In this example, value can be either a number or an object (at least, based on your example.) Unfortunately, this dives into the limits of an RPG -- RPG does not have the concept of a variable that can sometimes be one data type and sometimes be another. This sort of thing is commonplace in "weakly typed" languages such as JavaScript, Python, or PHP and is difficult (if not impossible) in strongly typed languages (RPG, Cobol, C, Java, etc)

    Since DATA-INTO is strictly a feature of RPG for mapping into a data structure that's determined at compile-time, you really can't use DATA-INTO here.

    What you CAN do is call Yajl's subprocedures. This is no problem at all for that. This is off the top of my head, untested, and I don't expect that it is 100% complete

    Code:
      docNode = yajl_stdin_load_tree(*on: errMsg);
      if docNode = *null;
        // handle error
      endif;
    
      filterNode = yajl_obj_get(docNode: 'filtersvalue');
      if filterNode = *null;
        // error: there was no 'filtersvalue'
      endif;
    
      i=0;
      dow YAJL_ARRAY_LOOP(filterNode: i: elemVal);
    
        nameNode = yajl_obj_get(elemVal: 'name');
        if nameNode = *null;
          name = '';
        else;
          nameNode = yajl_get_string(name);
        endif;
    
        startDate = *loval;
        endDate   = *hival;
        value     = 0;
    
        valueNode = yajl_obj_get(elemVal: 'value');
        if YAJL_IS_OBJECT(valueNode);
          rangeNode = yajl_obj_get(valueNode: 'dateRange');
          if rangeNode <> *null;
            startDate = yajl_obj_get(rangeNode: 'startDate');
            endDate   = yajl_obj_get(rangeNode: 'endDate'  );
          endif;
        elseif YAJL_IS_NUMBER(valueNode);
          value = yajl_get_number(valueNode);
        endif;
    
        // process this array element
      
      enddo;
    
      yajl_tree_free(docNode);


    Hope that makes sense.

    Comment

    Working...
    X