Partner400 Logo
    Creating JSON with YAJL
   

 

Below is the source listing for the example YAJL program in the article (JSONCRT2). We have also included an additional program (JSONCRT1) which generates the employee JSON document used as an example in the first part of the article.



JSONCRT1 - Generate employee JSON document

       //-----------------------------------------------------//
       // Test program that builds the JSON employee array
       //    shown in the article. The code is not descibed
       //    in the article but follows the same basic pattern
       //-----------------------------------------------------//

       ctl-opt  DftActgrp(*No)    BndDir('YAJL/YAJL')
                Option(*SrcStmt)  DecEdit('0.');

      /copy yajl/qrpglesrc,yajl_h

       dcl-s  i             Int(5);
       dcl-s  wait          Char(1);
       dcl-s  errMsg        VarChar(500) Inz;

       //-------------------------------------------------------------//
       // The following DS array and subsequent "fill" logic are used
       //    to avoid having to use data files to provide test data

       dcl-ds employees  Dim(3)  Qualified;
         firstName  Char(30);
         lastname   Char(30);
       end-ds;

       // Initialize the test data array
       employees(1).firstName = 'Jon';
       employees(1).lastname  = 'Paris';
       employees(2).firstName = 'Susan';
       employees(2).lastname  = 'Gantner';
       employees(3).firstName = 'Paul';
       employees(3).lastname  = 'Tuohy';

       //----------------------------//
       // Begin program's main logic

       yajl_genOpen(*On); // Start generation with "pretty" format

       yajl_beginObj();   // Start root object

       yajl_beginArray('employees');  // Begin employees array

       for i = 1 to %Elem(employees); // Process all employees
         // Create employee object complete with first and lastname variables
         yajl_beginObj();
         yajl_addChar('firstName': %Trim(employees(i).firstName));
         yajl_addChar('lastName': %Trim(employees(i).lastName));
         yajl_endObj();
       EndFor;

       yajl_endArray(); // Close the employee array

       yajl_endObj();   // And the root object

       // Generation is complete so write JSON to IFS file
       yajl_saveBuf('/Partner400/JSONEX1.json': errMsg );

       if errMsg <> '';  // Check if any error was detected
         Dsply 'Ooppppssss - problem!' ' ' Wait;
       EndIf;

       *InLr = *On;
 


JSONCRT2 - Main JSON Customer Document Generation Program

       //---------------------  JSONCRT2  -------------------------//
       //  This is the main example program as described in the
       //    article.
       //  Letters on the left hand side match those in the article
       //----------------------------------------------------------//
(E)    ctl-opt  DftActgrp(*No)    BndDir('YAJL')
                Option(*SrcStmt)  DecEdit('0.');

(F)   /copy yajl/qrpglesrc,yajl_h

       dcl-s  i             Int(5);
       dcl-s  wait          Char(1);
(G)    dcl-s  errMsg        VarChar(500) Inz;

       // This DS is included just as a comparison with
       //   JSON structure genrated by this program.

       // dcl-ds Customers;
       //   dcl-ds Customer Dim(3) Qualified;
       //      Id    Char(7);
       //      Name  Char(40);
       //      dcl-ds  Address;
       //       City   Char(30);
       //       State  Char(2);
       //     end-ds;
       //   end-ds;
       // end-ds;

       // Main Process Begins ...

(H)    yajl_genOpen(*On);


      // In the following code I have attempted to indent the yajl_ calls
      //   to indicate the shape of the structure being generated. Hopefully
      //   you will find it helpful.

(I)    yajl_beginObj();  // Start the root object

(J)      yajl_beginObj('Customers');

(K)        yajl_beginArray('Customer');

           for i = 1 to 3;

(L)         yajl_beginObj();
(M)            yajl_addChar('Id': %Char(i));
               yajl_addChar('Name': %Char(i));
(N)            yajl_beginObj('Address');
(O)              yajl_addChar('City': %Char(i));
                 yajl_addChar('State': %Char(i));
(P)            yajl_endObj();
             yajl_endObj();

           EndFor;

(Q)        yajl_endArray();  // End Customer array

         yajl_endObj();  // End Customers object

       yajl_endObj();  // End root object

(R)    yajl_saveBuf('/Partner400/JSONEX2.json': errMsg );

(S)    if errMsg <> '';
         Dsply 'Ooppppssss - problem!' ' ' Wait;
       EndIf;

       *InLr = *On;
 

     
Return to Home Page  

Want more information?
Got a question or comment about the site?
Please feel free to Contact Us at any time.}