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:

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
Journaling the IFS, SPLTOOL and Java Journaling the IFS, SPLTOOL and Java
Posted by August 4, 2021
BVSTools >> BVSTools Announcements >> Spooled File Tools (SPLTOOL) Specific Announcements
G4MSMAIL Now Allows Wildcard Attachments G4MSMAIL Now Allows Wildcard Attachments
Posted by June 10, 2021
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
G4G Update for Uploading Large Files G4G Update for Uploading Large Files
Posted by May 28, 2021
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
Can We Purchase License Keys for More Than One Year at a Time? Can We Purchase License Keys for More Than One Year at a Time?
Posted by May 21, 2021
BVSTools >> BVSTools Announcements
JOBWATCH 5.10 Released with New Feature to Monitor CPU Percentages JOBWATCH 5.10 Released with New Feature to Monitor CPU Percentages
Posted by May 10, 2021
BVSTools >> BVSTools Announcements >> Job Watch (JOBWATCH) Specific Announcements
Iseries Access data transfer Iseries Access data transfer
Posted by April 29, 2021
IBM Power Systems >> (QGPL) IBM i
YAJL - Parsing YAJL - Parsing
Posted by April 4, 2021
Programming >> RPG Programming
Create LPAR partition and install OS Create LPAR partition and install OS
Posted by March 24, 2021
IBM Power Systems >> (QGPL) IBM i
G4MS Now Allows User/Password Authentication Option G4MS Now Allows User/Password Authentication Option
Posted by March 16, 2021
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
IPDS printer file using CHRSIZ IPDS printer file using CHRSIZ
Posted by February 25, 2021
Programming >> RPG Programming

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).