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



Latest Posts:

BVSTools Now Offers Interface with Infor's ION APIs BVSTools Now Offers Interface with Infor's ION APIs
Posted by May 15, 2020
BVSTools >> BVSTools Announcements
More V7R4 IFS File CCSID Issues and The Fix More V7R4 IFS File CCSID Issues and The Fix
Posted by March 4, 2020
IBM Power Systems >> (QGPL) IBM i
Error Retrieving IP Address by Name Error Retrieving IP Address by Name
Posted by February 25, 2020
BVSTools >> BVSTools Software Discussion
Logging jobs that hit an outq Logging jobs that hit an outq
Posted by February 13, 2020
Programming >> CL Programming
GreenTools for Google Apps (G4G) v12.60 Released with Shared Drive Features and More... GreenTools for Google Apps (G4G) v12.60 Released with Shared Drive Features and More...
Posted by February 4, 2020
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
Allowing Requests over Port 80 For SSL Validation (ie, Namecheap, etc) Allowing Requests over Port 80 For SSL Validation (ie, Namecheap, etc)
Posted by January 31, 2020
Programming >> Web Programming
GreenTools for Slack (G4SLK) v3.00 Released GreenTools for Slack (G4SLK) v3.00 Released
Posted by January 17, 2020
BVSTools >> BVSTools Announcements >> GreenTools for Slack (G4SLK) Specific Announcements
Calling a QSH Command from RPG Calling a QSH Command from RPG
Posted by December 26, 2019
Programming >> RPG Programming
SPLTOOL Print Range (PRTRNG) Function Updated to Handle Spooled Files up to 999,999,999 Pages SPLTOOL Print Range (PRTRNG) Function Updated to Handle Spooled Files up to 999,999,999 Pages
Posted by December 14, 2019
BVSTools >> BVSTools Announcements >> Spooled File Tools (SPLTOOL) Specific Announcements
GreenTools for Microsoft Apps (G4MS) Updated to v6.00 - Now Uses Microsoft Graph APIs GreenTools for Microsoft Apps (G4MS) Updated to v6.00 - Now Uses Microsoft Graph APIs
Posted by November 24, 2019
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
V7R4 Changes CCSID of Compressed File Using PASE JAR Command - Here's The Fix V7R4 Changes CCSID of Compressed File Using PASE JAR Command - Here's The Fix
Posted by November 21, 2019
IBM Power Systems >> (QGPL) IBM i
Using GETURI to Make OAuth 2.0 Requests - Custom Headers for Access Tokens Using GETURI to Make OAuth 2.0 Requests - Custom Headers for Access Tokens
Posted by November 11, 2019
BVSTools >> BVSTools Software Discussion >> Get URI (GETURI) Specific Discussion
GreenTools for Microsoft Apps (G4MS) v5.00 Released with Updated OneDrive Support and 3rd Party Functionality GreenTools for Microsoft Apps (G4MS) v5.00 Released with Updated OneDrive Support and 3rd Party Functionality
Posted by October 20, 2019
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
BVSTools is Now Running V7R4M0 BVSTools is Now Running V7R4M0
Posted by September 28, 2019
BVSTools >> BVSTools Announcements
GreenTools for G Suite (G4G) Now Includes Send Mail Functionality (G4GSMAIL Addon) GreenTools for G Suite (G4G) Now Includes Send Mail Functionality (G4GSMAIL Addon)
Posted by August 27, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific 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).