ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

YAJL - Parsing

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

  • YAJL - Parsing

    Hello friends,

    I am trying to parse below JSON using YAJL. YAJLGEN generated below data structure but the issue i am facing is the number of arrays ex: KEY, CUSTOMER are not fixed. These arrays are returned for each field in the response. I am trying to avoid defining an array for each field from the response.

    Could you please, advise if there is a better way to read the below json and parse dynamic arrays. I tried using "yajl_array_loop", "yajl_array_elem" but i couldn't able to make it work in my program for some reason. Thank is in advance.



    dcl-ds jsonDoc qualified;
    errstatus packed(3) inz(0);
    dcl-ds ERRORS;
    num_KEY int(10) inz(0);
    KEY varchar(37) inz('') dim(1);
    num_CUSTOMER int(10) inz(0);
    CUSTOMER varchar(43) inz('') dim(2);
    end-ds;
    end-ds;

    ***************

    {
    "errstatus": 400,
    "errors": {
    "Key": [
    "The Key field is required."
    ],
    "Customer": [
    "The Customer field is required."
    ]
    }
    }

  • #2
    Do you mean by "... number of arrays ex: KEY, CUSTOMER are not fixed." that the element names Key and Customer are not fixed ? i.e. there may be many others with different names? If so then a loop using the basic YAJL APIs is your only option. You would need YAJL_OBJECT_LOOP to go through the elements (i.e. Key and Customer) and YAJL_ARRAY_LOOP to get the individual values within each of those elements. You cannot use DATA-INTO which appears to be what you are trying to do. It requires that all names are known in advance.

    I'm not competent enough with the JSON SQL support to know if that would help, but all examples that I have seen so far rely on the element names being known.

    Comment


    • uachievegmail
      uachievegmail commented
      Editing a comment
      Thank you very much for your reply. After making some code changes as shown below, I was able to parse the dynamic fields array using Scott's YAJL tool.

      num_errors = 0;
      i = 0;
      errors = yajl_object_find(docNode: 'errors');
      dow yajl_object_loop(errors: i: field : errorsNode);
      num_errors = i;
      errors_ds(i).errros_fieldname = %trim(field);
      j = 0;
      errorslist = yajl_object_find(errors : %trim(field) );
      dow YAJL_ARRAY_LOOP(errorslist :j:node );
      errors_ds(i).num_error_msgs = j;
      errors_ds(i).error_msgs(j).error_msg = yajl_get_string(node);
      enddo;
      enddo;

  • #3
    I don't fully understand what you are asking.

    But, you cannot use the YAJL subprocedures to work with something you parsed with DATA-INTO. If you need to use the subprocedures you will need to parse it with the subprocedures as well. You may be able to handle this entirely with DATA-INTO if you use the %HANDLER function -- its not clear if this would work, since I don't fully understand what you're asking. You definitely can do whatever you want if you use the subprocedures -- its just more complex to code.

    Comment


    • uachievegmail
      uachievegmail commented
      Editing a comment
      Hi Scott, Thank you very much for your reply. I am currently working on parsing an API response and was initially trying to use data-into as shown below
      I realized later that data-into is not a good way to parse the JSON response in this current scenario, Now I am using
      yajl_object_loop, yajl_array_loop to parse the response and I am parsing the data I needed from the response. Thank you very much for the YAJL tool. it's a lifesaver when it comes to parsing the JSON response.

      monitor;
      data-into apiresponse
      %data( ifsPathName
      : 'doc=file case=any countprefix=num_ trim=none' +
      ' allowmissing=yes allowextra=yes')
      %parser('YAJLINTO':


      {
      "errstatus": 400,
      "errors": {
      "Key": [
      "The Key field is required."
      ],
      "Customer": [
      "The Customer field is required."
      ]
      "ID": [
      "The ID field is required."
      ]
      }
      }
Working...
X