YAJL_OBJECT_LOOP not working on second level group

Discussions relating to the ScottKlement.com port of the open source YAJL JSON Reader/Generator. This includes the YAJL tool as well as the YAJLR4, YAJLGEN, YAJLINTO and YAJLDTAGEN add-ons from ScottKlement.com. http://www.scottklement.com/yajl/
Post Reply
ray.marsh
Posts: 7
Joined: Fri Aug 08, 2025 12:58 pm

YAJL_OBJECT_LOOP not working on second level group

Post by ray.marsh »

The YAJL_OBJECT_LOOP the body element works fine but does not work for the return_message element inside the body.
The YAJL_OBJECT_LOOP for the return_message returns nothing. The return_message pointer is not null.
The items pointer works fine in a YAJL_ARRAY_LOOP (not shown).

Code: Select all

{
  "statusCode": 200,
  "body": {
    "return_message": [
      {
        "message_long_text": "Order: 10014027 created.",
        "message_severity": 0,
        "message_text": "success",
        "msgid": "STS0001"
      }
    ],
    "items": [
      {
        "item_id": "1",
        "mfr_code": "ACI",
        "part_number": "172189",
        "list": 29.51,
        "sell": 13.82,
        "core": 0,
        "quantity_ordered": 1,
        "status": "success"
      },
      {
        "item_id": "2",
        "mfr_code": "FFH",
        "part_number": "PG2500F",
        "list": 7.79,
        "sell": 2.11,
        "core": 0,
        "quantity_ordered": 1,
        "status": "success"
      }
    ],
    "order_number": "10014027",
    "legacy_order_number": "38806743",
    "status": "success"
  }
}

Code: Select all

          docNode = YAJL_stmf_load_tree( JSON_Response : JSON_errMsg );
          body = YAJL_object_find(docNode: 'body');

           idx = 0;
           dow YAJL_object_loop(body : idx : keytext : elem) ;
             select ;
             when keytext = 'status';
               status = yajl_get_string(elem);
             when keytext = 'error_description';
               error_description = yajl_get_string(elem);
             when keytext = 'return_message';
               return_message = elem;
             when keytext = 'items';
               items = elem;
             endsl;
           enddo;

           idx = 0;
           dow YAJL_object_loop(return_message : idx : keytext : elem) ;
             select ;
             when keytext = 'message_long_text';
               message_long_text = yajl_get_string(elem);
             when keytext = 'message_severity';
               message_severity = YAJL_get_number(elem);
             when keytext = 'message_text';
               message_text = yajl_get_string(elem);
             when keytext = 'msgid';
               msgid = yajl_get_string(elem);
             other;
             endsl;
           enddo;                                                        
jonboy49
Posts: 251
Joined: Wed Jul 28, 2021 8:18 pm

Re: YAJL_OBJECT_LOOP not working on second level group

Post by jonboy49 »

I'll try to look at your code later, but is there a reason why you are using the "raw" YAJL APIs? I just ran your JSON through YAJLGEN, and the resulting DATA-INTO works perfectly on this document.
ray.marsh
Posts: 7
Joined: Fri Aug 08, 2025 12:58 pm

Re: YAJL_OBJECT_LOOP not working on second level group

Post by ray.marsh »

I appreciate you looking into this. I've tried several ideas and so far no luck.

There is a reason why I am using the "raw" YAJL APIs.

We have over a large number systems at V7R2 and they do not have the necessary pieces to use data-into. The compiler fails on data-into on the V7R2 release. From what I read it should work at 7.2 but it does not. 7.4 works fine but upgrading the systems isn't an option at this point nor is applying the necessary PTFs (if that's the issue). That's what I'm getting from operations.

So I will have to use the older methods for now, unfortunately.
jonboy49
Posts: 251
Joined: Wed Jul 28, 2021 8:18 pm

Re: YAJL_OBJECT_LOOP not working on second level group

Post by jonboy49 »

Your basic problem is that you are treating return_message as an object but it is an array. So you need to work through it using YAJL_array_loop and _then_ YAJL_OBJECT_LOOP for its elements. It appears that return_message is a single element so heaven knows why the doc was designed like this but ...

In other words, process return_message the same way as you handle items.
Scott Klement
Site Admin
Posts: 905
Joined: Sun Jul 04, 2021 5:12 am

Re: YAJL_OBJECT_LOOP not working on second level group

Post by Scott Klement »

Correct, it is an array, not an object.

If you want to loop through the contents of the array, by all means use yajl_array_loop() as jon suggested.

But if you just want to grab a single element, use yajl_array_elem().
ray.marsh
Posts: 7
Joined: Fri Aug 08, 2025 12:58 pm

Re: YAJL_OBJECT_LOOP not working on second level group

Post by ray.marsh »

The yajl_array_loop worked. Thank you.

I had tried it already but failed to reset the index parameter. When it failed I thought it was because return_message wasn't an array since it did not have [ ] in it. I was under the impression that square braces indicated a JSON array.
Post Reply