Forums >> Programming >> RPG Programming
Building JSON Data with RPG
by: bvstone

Jump to: 






bvstone

Building JSON Data with RPG

Posted:
Building JSON Data with RPG

I've been playing around lately with creating Android Apps with the MIT App Inventor.  A few years ago we used the App Inventor by Google (which was taken over by MIT) and thought it was fun.

I uses a "puzzle" like UI to build applications taking a lot of the complication out of it, as compared to using the Android SDK. 

The sample above shows the UI, and while fun, it does have it's shortfalls.  But we'll save that for another day.

In creating my app I found that using a DB server would make it a lot more interactive and feature filled.  So, I started writing CGI programs to output JSON data that could be retrieved with a call to a URI.  

One thing I needed to do was list users that were signed on.  So the first thing I did was create a template for the eRPG SDK that looks like the following:

/$section1
{
 
/$userlist
 "user/%count%/":"/%userid%/",
 "userdesc/%count%/":"/%desc%/"
 
/$comma
,

/$end
 "count":/%count%/
}

The template consists of 3 sections.  The first section is for the start of the JSON data.  Then for each user in the list, we will specify a field named user<count> and userdesc<count> where <count> is the index of each user.  Finally, we output the number of users in the list.

The RPG program for this looks like the following:

     H DFTACTGRP(*NO) BNDDIR('ERPGSDK') BNDDIR('QC2LE')
      ****************************************************************
      * Prototypes                                                   *
      ****************************************************************
      /COPY QCOPYSRC,P.ERPGSDK
      /COPY QCOPYSRC,P.SQL
      /COPY QCOPYSRC,P.DH
      ****************************************************************
     D USERDS        E DS                  EXTNAME(USERPF)
     D WEAPONDS      E DS                  EXTNAME(WEAPONPF) PREFIX(w_)
      *
     D inID            S                   LIKE(ID)
      *
     D i               S             10i 0
     D count           S             10i 0
     D tempDistance    S             13p 6
     D distance        S                   LIKE(w_DISTANCE)
     D myLat           S             13p 6
     D myLng           S             13p 6
      ****************************************************************
      /free
       #startup();
       #writeTemplate('stdhtmlheader.erpg');
       #loadTemplate('lstusr.erpg');
       #loadSection('section1');
       #writeSection();

       inID = #getData('id');

       exec sql
         select LAT, LONG into :myLat, :myLng from USERPF
           where ID = :inID;

       count = 0;

       exec sql
         declare C1 cursor for
           select ID, LAT, LONG from USERPF;

       exec sql
         open C1;

       exec sql
         fetch from C1
           into
             :ID, :LAT, :LONG;

       #loadSection('userlist');

       dow (xSQLState2 = Success_On_Sql);
         count += 1;

         tempDistance = #latLngDist(myLat:myLng:
                                    LAT:LONG);

         if (count > 1);
           #writeThisSec('comma');
         endif;

         #loadSection('userlist');
         #replaceData('/%count%/':%char(count));
         #replaceData('/%userid%/':ID);

         if (tempDistance < 1);
           eval(h) distance = (tempDistance * 1609.34);
           #replaceData('/%desc%/':%trim(ID) + ' (' +
                                   %char(distance) +
                                   ' meters)');
         else;
           eval(h) distance = tempDistance;
           #replaceData('/%desc%/':%trim(ID) + ' (' +
                                   %char(distance) +
                                   ' miles)');
         endif;

         #writeSection();

         exec sql
           fetch from C1
             into
               :ID, :LAT, :LONG;
       enddo;

       exec sql
         close C1;

       if (count > 0);
         #writeThisSec('comma');
       endif;

       #loadSection('end');
       #replaceData('/%count%/':%char(count));
       #writeSection();

       #cleanup();
       *INLR = *ON ;
      /end-free

Now, the list contains a user id as well as a description (which in this case we're including the distance from another user, since this is a location based application).

The JSON when returned looks like this:

{
   "user1":"bvstone",
   "userdesc1":"bvstone (.00 meters)",
   "user2":"wilmar",
   "userdesc2":"wilmar (285.20 miles)",
   "user3":"galapagos",
   "userdesc3":"galapagos (3100.04 miles)",
   "user4":"test",
   "userdesc4":"test (4.62 miles)",
   "count":4
}

Now, structurally we would have probably done this a little differently, but because the App Inventor can't really handle nested lists very well, we chose this method, and it works pretty well.

To parse the JSON we use the tools included in the App Developer.  This is what the "code" looks like:

First we get the number of items in the list.  Then, for each item, we read the name/value pairs and place them in a list so they are easier to work with in the structure of the App Inventor.  In this case, we use the list to populate a "list picker" object so that we can display a list of users to choose from.

So as you can see, tools like the eRPG SDK or CGIDEV2 are very useful for more than just web pages.  With the use of web services becoming much more prevalent, being creative can make you look like a hero!

And with the data formats changing quickly, from XML to JSON to whatever is next, we know at least the basic layout will allow us to hopefully only edit our templates in order to change the data format, or use a different format.


Last edited 09/08/2014 at 11:34:53




Reply




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