bvstone

Parsing JSON using RPG with Scott Klement's Port of YAJL (Yet Another JSON Library) - Part 4

Posted:

Parsing JSON using RPG with Scott Klement's Port of YAJL (Yet Another JSON Library) - Part 4

We are so excited about the YAJL JSON parser port that we decided to throw together an online demo for it (which there is a link to at the end of this article).

We already had one set up for our JSONTOOL parser, but since we've decided to move all future development over to use the YAJL parser we thought we better put something together for it as well.

One issue we ran into when putting together the YAJL parsing demo was we didn't have a way to say "get this value" from the JSON.  When we built our JSON parser we had a slightly different interaction method than with the YAJL parser.

For our parser we used the format:

node[index]:node[index]...

This means that we would use a node name followed by an optional index, and then followed by optional nodes down the line.  So, we need to convert this notation so that it returns the correct node from the JSON data when we are parsing it with the YAJL parser.

What we came up with was the following:

     H DFTACTGRP(*NO) BNDDIR('ERPGSDK') BNDDIR('YAJL')
      ****************************************************************
      * Imports
      ****************************************************************
      /include yajl_h
      ****************************************************************
      * Prototypes                                                   *
      ****************************************************************
      /COPY QCOPYSRC,P.ERPGSDK
      /COPY QCOPYSRC,P.HTTPSTD
      ****************************************************************
      * Local Prototypes                                             *
      ****************************************************************
     D #getNode        PR              *
     D  InString                  65535    Value Varying
      *
     D #getNodeIndex   PR            10i 0
     D  InOutString               65535    Varying
      ****************************************************************
      * Data read in from web page
     D variable        S           1024    Varying
     D json            S          65535
      *
      * Work Variables
     D docNode         s                   like(yajl_val)
     D node            s                   like(yajl_val)
      *
     D errMsg          s            500a   Varying
      *
     D data            S          65535    Varying
     D dataSize        S             10i 0
      ****************************************************************
      /free
       #startup();
       #setImmediate(*ON);
       #writeTemplate('stdhtmlheader.erpg');
       variable = #getData('variable');
       json = #getData('json');

       if (json <> ' ');
         docNode = yajl_buf_load_tree(%addr(json):%len(%trimr(json)):errMsg );

         if (docNode <> *NULL);
           #WrStout('JSON Size=' + %char(%len(%trimr(json))) + '<br>');

           node = #getNode(variable);
           data = ' ';
           dataSize = 0;

           select;
           when (YAJL_IS_STRING(node));
             data = yajl_get_string(node);
           when (YAJL_IS_NUMBER(node));
             data = %char(yajl_get_number(node));
           when (YAJL_IS_TRUE(node));
             data = 'true';
           when (YAJL_IS_FALSE(node));
             data = 'false';
           when (YAJL_IS_OBJECT(node));
             data = 'JSON Object';
             dataSize = YAJL_OBJECT_SIZE(node);
           endsl;

           if (dataSize = 0);
             dataSize = %len(data);
           endif;

           #WrStout('Object Value=' + data + '<br>');
           #WrStout('Object Size=' + %char(dataSize) + '<br>');
         endif;

       else;
         errMsg = 'No JSON data';
       endif;

       #WrStout('Error=' + errMsg + '<br>');

       yajl_tree_free(docNode);
       #cleanup();

       *INLR = *on;
      /end-free
      *//////////////////////////////////////////////////////////////*
      * #getNode - Get the Node Requested                            *
      *                                                              *
      * Input:                                                       *
      *   InString - The String representing the node to retrieve.   *
      *               If the JSON string starts as an array, use     *
      *               ROOT[index] as the first part of the request.  *
      *                                                              *
      *              examples:                                       *
      *              customerList[1]:firstName                       *
      *              ROOT[2]:firstName                               *
      *              data:color                                      *
      *                                                              *
      * Returns:                                                     *
      *   Pointer to node requested                                  *
      *                                                              *
      *//////////////////////////////////////////////////////////////*
     P #getNode        B                   EXPORT
      *--------------------------------------------------------------*
     D #getNode        PI              *
     D  InString                  65535    Value Varying
      *--------------------------------------------------------------*
     D i               S             10i 0
     D j               S             10i 0
     D curTag          S          65535    Varying
     D curTagIndex     S             10i 0
     D localNode       S                   like(yajl_val)
      *--------------------------------------------------------------*
      /free
       i = %scan(':':InString);
       j = 1;
       localNode = docNode;

       dow (i > 0);

         if (i > j); //Do this in case someone puts :: in the string
           curTag = %subst(InString:j:i-j);
           exsr $GetPointer;
         endif;

         j = (i + 1);
         i = %scan(':':InString:j);
       enddo;

       curTag = %subst(InString:j);
       exsr $GetPointer;

       return localNode;

       //*--------------------------------------------------------------*
       //* Get Pointer
       //*--------------------------------------------------------------*
       BegSr $GetPointer;

         curTagIndex = #getNodeIndex(curTag);

         select;
         when (curTagIndex < 0);
           errMsg = 'Error retrieving object index!';
           return *NULL;
         when (curTagIndex > 0);

           if (curTag <> 'ROOT');
             localNode = YAJL_object_find(localNode:curTag);
           endif;

           if (YAJL_IS_ARRAY(localNode));

             if (curTagIndex <= YAJL_ARRAY_SIZE(localNode));
               localNode = YAJL_ARRAY_ELEM(localNode:curTagIndex);
             else;
               errMsg = 'Index ' + %char(curTagIndex) +
                        ' out of range for ' + curTag + ' in ' +
                        variable + '.';
               return *NULL;
             endif;

           else;
             errMsg = curTag + ' is not an array.';
             return *NULL;
           endif;

         other;
           localNode = YAJL_object_find(localNode:curTag);

           if (YAJL_IS_ARRAY(localNode));
             errMsg = curTag + ' is an array.  Index ' +
                      %char(curTagIndex) + ' is invalid.';
             return *NULL;
           endif;

         endsl;

         if (localNode = *NULL);
             errMsg = curTag + ' not found in ' + variable + '.';
             return localNode;
         endif;

       EndSr;

       //*--------------------------------------------------------------*
       BegSr *PSSR;

         errMsg = 'getNode Internal error!';
         RETURN *NULL;

       EndSr;
       //*--------------------------------------------------------------*
      /end-free
     P #getNode        E
      *//////////////////////////////////////////////////////////////*
      * #getNodeIndex - Get The Index of the Node Requested          *
      *                                                              *
      * Input/Output:                                                *
      *   InOutString - The single node name with or without index   *
      *                  to retrieve the index for.                  *
      *                                                              *
      *                 examples:                                    *
      *                 customerList[1]                              *
      *                 ROOT[2]                                      *
      *                 color                                        *
      *                                                              *
      *                 This value will be returned minus the index  *
      *                  information when complete.                  *
      *                                                              *
      *                 examples:                                    *
      *                 customerList                                 *
      *                 ROOT                                         *
      *                 color                                        *
      *                                                              *
      * Returns:                                                     *
      *   Index of the item, -1 for error, 0 for a node name that    *
      *    does not contain an index.                                *
      *                                                              *
      *//////////////////////////////////////////////////////////////*
     P #getNodeIndex   B                   EXPORT
      *--------------------------------------------------------------*
     D #getNodeIndex   PI            10i 0
     D  InOutString               65535    Varying
      *--------------------------------------------------------------*
     D i               S             10i 0
     D j               S             10i 0
     D index           S             10i 0
      *--------------------------------------------------------------*
      /free
       index = 0;
       i = %scan('[':InOutString);
       j = 1;

       if (i > 1);
         i = (i + 1);
         j = %scan(']':InOutString:i);

         if (j > i);
           Monitor;
             index = %int(%subst(InOutString:i:j-i));
           On-Error;
             index = 1;
           EndMon;
         endif;

         InOutString = %subst(InOutString:1:i-2);
       endif;

       return index;

       //*--------------------------------------------------------------*
       BegSr *PSSR;

         return -1;

       EndSr;
       //*--------------------------------------------------------------*
      /end-free
     P #getNodeIndex   E

The basic functionality of this application is as follows:

  • Read in the JSON data
  • Read in the string that represents the node we want to retrieve the value for
  • Go through each node (separated by a colon) until we reach the end, then return a pointer to that last node
  • Retrieve the value of the node and display it

Most of the code included is used to parse the node string that is passed in.  For example, if we pass in the following node string:

customerList[2]:firstName

We first need to find the second customerList object.  Once we have that, we retrieve the firstName object from the specific customerList object.

That means looping through our input variable looking for a colon (:) until we find the end of the string.

For each object name in the input variable we retrieve the pointer to that specific node.  If the object is an array (denoted by [index]) that index is retrieved using the #getNodeIndex() subprocedure.  But, not only does this subprocedure return the index, it also updates the tag name and removes any array index from it.  So, if we sent in CustomerList[4] we will retrieve 4 as the index, and the object name is changed to CustomerList.

Once we've parsed our way down the JSON "tree" we end up with either a NULL value (which means the JSON object wasn't found) or a pointer to the JSON object which we can then use to retrieve the actual value from 

The demo can be see by clicking here.

Related Articles:


Last edited 10/21/2021 at 09:42:05




Reply




© Copyright 1983-2024 BVSTools
GreenBoard(v3) Powered by the eRPG SDK, MAILTOOL Plus!, GreenTools for Google Apps, jQuery, jQuery UI, BlockUI, CKEditor and running on the IBM i (AKA AS/400, iSeries, System i).