ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Parse JSON

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

  • Parse JSON

    I'?m struggling with parsing JSON data that was returned from a web service call. Below is a copy of the JSON. I?m using YAJL programs that were retrieved from Scott Klement's website. I can successfully read the values for MessageID, MessageType, MessageTime, ApiVersion, but when I get to the Payload item, I'm not able to parse any additional information. If I remove Payload from the JSON file then I can parse the entire JSON file including the Msg & Severity elements in the ResponseMessages array. What is different about Payload object in the JSON format that prevents me from parsing the JSON data?

    When I use the YAJL_object_find(docNode: 'Payload') function a null value is returned. Any help is appreciated.

    Take care,

    Doug

    {
    "MessageId": "780f064a-4060-4197-a8d7-fa4c6fa8dbf0",
    "MessageType": "LoadLocation",
    "MessageTime": "2016-11-04T14:46:02.3266448+00:00",
    "ApiVersion": "1.1",
    "Payload": {
    "ResponseMessages": [{
    "Msg": "Message Received and Queued for Processing.",
    "Severity": "INFORMATION"
    }]
    }
    }

  • #2
    I coded this up and tried it, it worked without error, I got the contents of Msg without problems.

    Code:
    **FREE
    ctl-opt dftactgrp(*no) actgrp(*new) bnddir('YAJL');
    
    /copy yajl_h
    
    dcl-s doc varchar(1000);
    dcl-s docNode like(yajl_val);
    dcl-s node like(yajl_val);
    dcl-s errMsg varchar(500);
    
    *inlr = *on;
    
    doc = '{+
            "MessageId": "780f064a-4060-4197-a8d7-fa4c6fa8dbf0",+
            "MessageType": "LoadLocation",+
            "MessageTime": "2016-11-04T14:46:02.3266448+00:00",+
            "ApiVersion": "1.1",+
            "Payload": {+
              "ResponseMessages": [{+
                "Msg": "Message Received and Queued for Processing.",+
                 "Severity": "INFORMATION"+
              }]+
            }+
          }';
    
     docNode = yajl_buf_load_tree( %addr(doc:*data): %len(doc): errMsg);
     if errMsg <> '';
        showMessage(errMsg);
        return;
     endif;
    
     node = yajl_object_find(docNode:'Payload');
     node = yajl_object_find(node: 'ResponseMessages');
     node = yajl_array_elem(node: 1);
     node = yajl_object_find(node: 'Msg');
    
     showMessage(yajl_get_string(node));
    
     yajl_tree_free(docNode);
     return;
    
     dcl-proc showMessage;
    
        dcl-pi *n;
           msg varchar(500) const;
        end-pi;
    
        dcl-pr QUILNGTX extPgm('QSYS/QUILNGTX');
           text      char(500) const;
           length    int(10)   const;
           msgid     char(7)   const;
           qualmsgf  char(20)  const;
           errorCode char(8)   const;
        end-pr;
    
        QUILNGTX( msg
                : %len(msg)
                : *blanks
                : *blanks
                : x'00000000' );
    
     end-proc;

    Comment

    Working...
    X