ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

YAJL - find array inside of array element

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

  • YAJL - find array inside of array element

    I'm wondering if anyone has done something like this with YAJL? I return an array of data from the browser which contains an array. Maybe it will make better sense visually? Our freight provider will use this tool to build shipments. Each row is a product and will have one or many pallets associated with it.

    Click image for larger version

Name:	lineitems.png
Views:	462
Size:	42.4 KB
ID:	150466

    The data is sent to my RPG program like below. At this point I'm able to get into the lineItems array no problem. It's when I then try to get the dims. While in the YAJL_OBJECT_LOOP I hit they dims key, but that's not really what I want. I've tried YAJL_OBJECT_FIND to look for dims, but that returns null. If I need to, I can re-work this to send a lineItems array and dims array separately, but I wouldn't mind getting this working!

    Click image for larger version

Name:	lineitemsjson.png
Views:	473
Size:	72.7 KB
ID:	150467

    JSON snippet, if that helps:

    Code:
    {
        "shipment": {
            "action": "Save",
            "businessUnit": "xxxxxxxxxxxxxxxx",
            "mode": "LTL",
            "lineItems": [{
                "oloqty": 1,
                "pgdesc": "SUPER BIKE 2 42\" DELUXE",
                "pgprdc": "BETM 024713N",
                "ptdesc": "SB2-42\" (DLX)",
                "ptinmt": "BETM 024713N",
                "ptoutm": "SB2-42\" (DLX)",
                "ptqtye": 1,
                "weight": 1657,
                "dims": [{
                    "ptnwgt": 412,
                    "ptgvol": 0,
                    "pthigh": 84,
                    "ptleng": 48,
                    "pttvoc": "",
                    "ptwidt": 36,
                    "ptqtye": 1
                }, {
                    "ptnwgt": 245,
                    "ptgvol": 0,
                    "pthigh": 48,
                    "ptleng": 38,
                    "pttvoc": "",
                    "ptwidt": 69,
                    "ptqtye": 1
                }, {
                    "ptnwgt": 1000,
                    "ptgvol": 0,
                    "pthigh": 48,
                    "ptleng": 48,
                    "pttvoc": "",
                    "ptwidt": 48,
                    "ptqtye": 1
                }]
            }, {
                "oloqty": 1,
                "pgdesc": "SNO CROSS 42\" DELUXE",
                "pgprdc": "BETM 025401N",
                "ptdesc": "SNO-X - 42\" (DELUXE)",
                "ptinmt": "BETM 025401N",
                "ptoutm": "SNO-X - 42\" (DELUXE)",
                "ptqtye": 1,
                "weight": 585,
                "dims": [{
                    "ptnwgt": 368,
                    "ptgvol": 0,
                    "pthigh": 70,
                    "ptleng": 46,
                    "pttvoc": "",
                    "ptwidt": 32,
                    "ptqtye": 1
                }, {
                    "ptnwgt": 217,
                    "ptgvol": 0,
                    "pthigh": 42,
                    "ptleng": 38,
                    "pttvoc": "",
                    "ptwidt": 69,
                    "ptqtye": 1
                }]
            }, {
                "oloqty": 1,
                "pgdesc": "BBHD 80\" MONITOR STAND",
                "pgprdc": "BETM 025432N",
                "ptdesc": "80\" STAND",
                "ptinmt": "BETM 025432N",
                "ptoutm": "80\" STAND",
                "ptqtye": 0,
                "weight": 155,
                "dims": [{
                    "ptnwgt": 155,
                    "ptgvol": 0,
                    "pthigh": 56,
                    "ptleng": 44,
                    "pttvoc": "",
                    "ptwidt": 35,
                    "ptqtye": 0
                }]
            }, {
                "oloqty": 1,
                "pgdesc": "ALIENS ARMAGEDDON 55\" DX",
                "pgprdc": "BETM 025765N",
                "ptdesc": "ALIENS 55 DESCRIPTION",
                "ptinmt": "BETM 025765N",
                "ptoutm": "ALIENS 55\"",
                "ptqtye": 1,
                "weight": 485,
                "dims": [{
                    "ptnwgt": 485,
                    "ptgvol": 0,
                    "pthigh": 96,
                    "ptleng": 48,
                    "pttvoc": "",
                    "ptwidt": 48,
                    "ptqtye": 1
                }]
            }]
        }
    }
    Thanks in advance.
    Mike
    Your friends list is empty!

  • #2
    I'm trying to understand how I can help you. I see your JSON document, and it looks pretty straightforward. (With names like "oloqty" and "ptnwgt", etc it looks like something an RPG programmer would make!)

    I'm not understanding where you are stuck. This is what you say:

    At this point I'm able to get into the lineItems array no problem. It's when I then try to get the dims. While in the YAJL_OBJECT_LOOP I hit they dims key, but that's not really what I want. I've tried YAJL_OBJECT_FIND to look for dims, but that returns null.



    This is where I'm lost. You say you want to get "the dims", but when YAJL_OBJECT_LOOP gives it to you, it's not what you want. Why not? You say you try with YAJL_OBJECT_FIND but you get null, which makes me think you're not querying it's parent element, you're querying something else, but you didn't give the specifics, so I don't know for sure.

    Can you explain why you don't want "dims" from YAJL_OBJECT_LOOP? Can you show the code with YAJL_OBJECT_FIND that is not working, including how you retrieve the parent element?

    Comment


    • #3
      Scott - I think I'm okay now. You're reply made me rethink where I was at in the JSON document and what I was trying to pull in.

      Code:
      lineItemsArray = YAJL_object_find(subNode: 'lineItems');
      i = 0;                                                  
      dow YAJL_ARRAY_LOOP( lineItemsArray :i :node );        
        j = 0;                                                
        dow YAJL_OBJECT_LOOP( node: j: key: val);            
          exsr loadRowElements;                              
        enddo;                                                
      enddo;
      Previously I was using subNode in the YAJL_object_find(). I switched it to node and I have what I need.

      Code:
      begsr loadRowElements;                                                  
      
          select;                                                              
            when (key = 'dims');                                              
              dimsArray = YAJL_object_find([COLOR=#FF0000][B]node[/B][/COLOR]: 'dims');                      
              ii = 0;                                                          
              dow YAJL_ARRAY_LOOP( dimsArray :ii :node );                      
                jj = 0;                                                        
                dow YAJL_OBJECT_LOOP( node: jj: key: val);                    
                select;                                                        
                when (key = 'pthigh');                                        
                  shipment.lineItems(i).dims(ii).height = yajl_get_number(val);
                when (key = 'ptnwgt');                                        
                  shipment.lineItems(i).dims(ii).weight = yajl_get_number(val);
                when (key = 'ptwidt');                                        
                  shipment.lineItems(i).dims(ii).width = yajl_get_number(val);
                when (key = 'ptnwgt');                                        
                  shipment.lineItems(i).dims(ii).weight = yajl_get_number(val);
                endsl;                                                        
                enddo;                                                        
              enddo;

      Thank you!
      Your friends list is empty!

      Comment

      Working...
      X