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



Latest Posts:

GreenTools for Google Apps (G4G) v15.00 Now Offers Functions to Bypass Registration Command and BVSTools Landing Page GreenTools for Google Apps (G4G) v15.00 Now Offers Functions to Bypass Registration Command and BVSTools Landing Page
Posted by May 3, 2022
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
How Do I Switch From MAILTOOL Plus to GreenTools for Google or Microsoft Office 365? How Do I Switch From MAILTOOL Plus to GreenTools for Google or Microsoft Office 365?
Posted by April 18, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
PTFs Issued for SSL/TLS Issues PTFs Issued for SSL/TLS Issues
Posted by March 12, 2022
IBM Power Systems >> PTF Watch
Google Dropping Support for Google Dropping Support for "Less Secure Apps" May 30th, 2022. What Does This Mean for Your IBM i Email?
Posted by March 4, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Have You Installed a New Version of MAILTOOL and Now Things Are Acting Different?  Check the Command Defaults! Have You Installed a New Version of MAILTOOL and Now Things Are Acting Different? Check the Command Defaults!
Posted by February 28, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Using MAILTOOL Plus on V7R1, or Any OS Using TLS 1.1 or Older Using MAILTOOL Plus on V7R1, or Any OS Using TLS 1.1 or Older
Posted by January 27, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
BVSTools ILE Functions Being Updated to Remove Hashtag (#) from Function Names BVSTools ILE Functions Being Updated to Remove Hashtag (#) from Function Names
Posted by December 30, 2021
BVSTools >> BVSTools Announcements
GETURI v12.00 Released Removing Beginning # (Hashtag) From Function Names GETURI v12.00 Released Removing Beginning # (Hashtag) From Function Names
Posted by December 28, 2021
BVSTools >> BVSTools Announcements >> Get URI (GETURI) Specific Announcements
Is any of BVSTools Software Affected by the log4j exploit? Is any of BVSTools Software Affected by the log4j exploit?
Posted by December 20, 2021
BVSTools >> BVSTools Software Discussion
GreenTools for Microsoft Apps (G4MS) Updated to Allow Downloads, Deletes, and Sharing of Files GreenTools for Microsoft Apps (G4MS) Updated to Allow Downloads, Deletes, and Sharing of Files
Posted by December 17, 2021
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
SSL Handshake Errors with GETURI, MAILTOOL and GreenTools Products SSL Handshake Errors with GETURI, MAILTOOL and GreenTools Products
Posted by October 18, 2021
BVSTools >> BVSTools Software Discussion
MAILTOOL Updated to Retry Sending when GSK SSL Handshake Error 415 (GSK_ERROR_BAD_PEER) is Encountered MAILTOOL Updated to Retry Sending when GSK SSL Handshake Error 415 (GSK_ERROR_BAD_PEER) is Encountered
Posted by August 19, 2021
BVSTools >> BVSTools Announcements >> eMail Tool (MAILTOOL) Specific Announcements
MAILTOOL Updated to Allow List-Unsubscribe and User Defined Headers MAILTOOL Updated to Allow List-Unsubscribe and User Defined Headers
Posted by August 13, 2021
BVSTools >> BVSTools Announcements >> eMail Tool (MAILTOOL) Specific Announcements
AWS signing process in as400 AWS signing process in as400
Posted by August 13, 2021
Programming >> Web Programming
2022 License Price Increase and Consulting Update for Non-Software Specific Assistance 2022 License Price Increase and Consulting Update for Non-Software Specific Assistance
Posted by August 9, 2021
BVSTools >> BVSTools Announcements

Reply




Copyright 1983-2020 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).