IBM i Global

IBM i Global

Connect, learn, share, and engage with IBM Power.

 View Only
Expand all | Collapse all

How can I employ RPGLE to generate a JSON document in IFS

  • 1.  How can I employ RPGLE to generate a JSON document in IFS

    Posted Mon April 10, 2023 03:48 PM

    Hello All,

    Good Morning to you. how are you?

    Do you simply suggest I write each row to a PF with all the delimiters & then convert PF to IFS thru Copy To Import File (CPYTOIMPF)  ?

    Or will you please share with me anything that you may have used in past to generate a JSON document in IFS thru RPG.



    ------------------------------
    Mohan Eashver
    ------------------------------


  • 2.  RE: How can I employ RPGLE to generate a JSON document in IFS

    Posted Mon April 10, 2023 04:47 PM

    Hi,

    IMHO the easiest way to generate JSON is using embedded SQL and the JSON_xxx functions. Then write it out with IFS_WRITE.

    HTH



    ------------------------------
    Daniel Gross
    ------------------------------



  • 3.  RE: How can I employ RPGLE to generate a JSON document in IFS

    Posted Mon April 10, 2023 05:25 PM

    Thank you Daniel. Which OS version is required for JSON_xxx functions ?
    We are on 7.1 yet.



    ------------------------------
    Mohan Eashver
    ------------------------------



  • 4.  RE: How can I employ RPGLE to generate a JSON document in IFS

    Posted Mon April 10, 2023 08:07 PM

    Dear Mohan

    You should do a Google search with "ibm i json" and you will see many informationn on this.     Search with IBM i (or AS/400) + json in Youtube produces quite a few videoes on this as well.

    As for SQL JSON-XXXX functions, I checked in IBM i documentation web site and found that all these information started in IBM i 7.2.  But you should try it in your system just in case you may be lucky and it does not take much effort to try.   Here is one good article on JSON-XXX : DB2 for i SQL JSON_OBJECT Essential Cheat Sheet at  https://blog.faq400.com/en/db2-for-i/db2-for-i-sql-json_object-essential-cheat-sheet-2/.  Example no. 7 is what you should look at.



    ------------------------------
    Education is not the learning of facts but the training of the mind to think. -- Albert Einstein.
    ------------------------------
    Satid S.
    ------------------------------



  • 5.  RE: How can I employ RPGLE to generate a JSON document in IFS

    Posted Tue November 21, 2023 05:55 PM

    Hi Satid,

    As per the above example7, I have created the JSON file in IFS folder like below.
    Unfortunately, I am getting the symbol '£' instead of '[' while using the JSON_ARRAYAGG function. Could you please help me out of this ASAP.
     
     
    dcl-s OUTFILE   sqltype(CLOB_FILE)   CCSID(1208);                  
    Clear OutFile;                                                     
    Outfile_Name = '/home/json.json';                                
    Outfile_NL = %len(%trimr(Outfile_Name));                           
    Outfile_FO = SQFCRT;                                               
    Exec SQL                                                                                                                             
       select json_object('CREDITS' value  json_arrayagg(json_object(  
       'CUSID' value CUSNUM, 'FSTNAME' value trim(FNAME),              
        'DATE' value  trim(DATE), 'LSTNAME' value trim(LNAME),         
        'CRDLMT' value trim(CREDLMT))))                                
        into :OUTFILE from mftdrsp20.CREDITS; 
     
    Result:
    {"CREDITS":£{"CUSID":"10091","FSTNAME":"WILLIAMSON","DATE":"20231120","LSTNAME":"KANE","CRDLMT":"990099"},{"CUSID":"10092","FSTNAME"
    :"LEWIS","DATE":"20231025","LSTNAME":"JOHN","CRDLMT":"900099"},{"CUSID":"10093","FSTNAME":"DANIEL","DATE":"20231118","LSTNAME":"MICH
    EL","CRDLMT":"990000"},{"CUSID":"10094","FSTNAME":"ANTONY","DATE":"20230925","LSTNAME":"MARK","CRDLMT":"800000"},{"CUSID":"10095","F
    STNAME":"ANDRICK","DATE":"20231029","LSTNAME":"MARKFED","CRDLMT":"900000"},{"CUSID":"10096","FSTNAME":"SMITH","DATE":"20231111","LST
    NAME":"STEVE","CRDLMT":"850000"},{"CUSID":"10097","FSTNAME":"BEJOS","DATE":"20231202","LSTNAME":"JEF","CRDLMT":"700000"},{"CUSID":"1
    0098","FSTNAME":"THOMAS","DATE":"20231208","LSTNAME":"EVANS","CRDLMT":"790000"},{"CUSID":"10099","FSTNAME":"AURTHOR","DATE":"2023113
    0","LSTNAME":"ADAMS","CRDLMT":"780000"},{"CUSID":"10100","FSTNAME":"BUTTLER","DATE":"20231025","LSTNAME":"JOE","CRDLMT":"850000"}]}


    ------------------------------
    Siva Prasad
    ------------------------------



  • 6.  RE: How can I employ RPGLE to generate a JSON document in IFS

    Posted Wed November 22, 2023 01:24 AM

    Hi there,

    as already mentioned on April, 11th you should give the ILE Open Source Framework noxDB a try.

    This is for free and open Source and you can do anything with Json and XML inside RPG/Cobol/C/C** on IBM I and it is faster than the IBM SQL functions and much more flexible (although the IBM functions get better of course).

    Your problem looks like a typical CCSID Problem.

    The other question is, do you really need the Json in IFS or what do you want to do with that Json?
    With noxDB you can directly send it via the ILE function json_httpRequest() to another Service/Webserver.

    Give it a try. If you need any help with it, you can write here or contact me.

    Either way or the other I wish you good luck with your project.

    Kind regards

    Markus



    ------------------------------
    Kind regards

    Markus A. Litters
    ------------------------------



  • 7.  RE: How can I employ RPGLE to generate a JSON document in IFS

    Posted Wed November 22, 2023 02:20 AM

    Marcus, the SQL JSON functions work perfectly data is returned in UTF-8, but in RPG the SQLTYPE Definitions ignore the CCSID Key word! 



    ------------------------------
    Birgitta Hauser
    Database and Software Engineer
    Selfemployed - Modernization-Education-Consulting on IBM i
    Kaufering
    +49 170 5269964
    ------------------------------



  • 8.  RE: How can I employ RPGLE to generate a JSON document in IFS

    Posted Wed November 22, 2023 02:26 AM

    Hi Birgitta,

    of course they do but as you mention below at the end it is a CCSID Problem. From wherever it comes. It looks like it is the IFS file as you say.

    But the question is, what he really wants to do with the Json. Normally Json is not used to transfer it via Email or Ftp ;-)



    ------------------------------
    Markus A. Litters
    ------------------------------



  • 9.  RE: How can I employ RPGLE to generate a JSON document in IFS

    Posted Wed November 22, 2023 04:30 AM

    Hi Markus,

    Thanks for the reply it would be helpful for me.

    Q: do you really need the Json in IFS or what do you want to do with that Json?

    A: Yes, we need the Json in IFS to send this to UNIX server.

    I will give a try with your solution. If I need any help,  I can write here or contact you.

    Kindly Regards,

    Siva



    ------------------------------
    Siva Prasad
    ------------------------------



  • 10.  RE: How can I employ RPGLE to generate a JSON document in IFS

    Posted Wed November 22, 2023 06:24 AM

    Hi Siva,

    your welcome.

    The question in detail is how you want to send it to the unix server. As already mentioned you can use for example json_httpRequest() within noxDB to send it directly to the unix Server if he has a Webservice that retrieves that Json. Json is normally used within REST services. Therefore I ask.

    On the other hand you can use IceBreak or the open Source Framework ILEASTIC to host your own RPG Webservices and the Unix Server can access that.



    ------------------------------
    Markus A. Litters
    ------------------------------



  • 11.  RE: How can I employ RPGLE to generate a JSON document in IFS

    Posted Wed November 22, 2023 02:18 AM
    Edited by Birgitta Hauser Wed November 22, 2023 02:25 AM

    Check your IFS File, I assume it is not generated as UTF-8. CCSID is ignored for the SQLTYPE variables. (Might be a good idea to propose an IBM idea for that).

    You may try to generate an empty IFS file with CCSID 1208 using the IFS_WRITE_UTF8 IBM service:

    Exec SQL Call Qsys2.IFS_Write_UTF8(Path_Name =>   '/Your/IFS/File.json'
                         Line           =>   ''

                                      Overwrite      =>   'REPLACE');

     and then set the _FO field to 'SQFAPP' and perform the SELECT ... INTO.



    ------------------------------
    Birgitta Hauser
    Database and Software Engineer
    Selfemployed - Modernization-Education-Consulting on IBM i
    Kaufering
    +49 170 5269964
    ------------------------------



  • 12.  RE: How can I employ RPGLE to generate a JSON document in IFS

    Posted Mon April 10, 2023 08:12 PM
    Edited by Satid Singkorapoom Mon April 10, 2023 11:05 PM
      |   view attached

    I just found a presentation from the great Scott Klement on doing this from RPG and YAJL JSON reader/writer tool and attach the file for you herewith.   Do a search in Youtube with "yajl rpg" and you will see several videoes on this as well.

    You can download YAJL from Scott Klement's web site : YAJL (Yet Another JSON Library) at  https://www.scottklement.com/yajl/


    ------------------------------
    Education is not the learning of facts but the training of the mind to think. -- Albert Einstein.
    ------------------------------
    Satid S.
    ------------------------------

    Attachment(s)

    pdf
    Working with JSON in RPG.pdf   380 KB 1 version


  • 13.  RE: How can I employ RPGLE to generate a JSON document in IFS

    Posted Tue April 11, 2023 01:14 AM
    Edited by Markus Litters Tue April 11, 2023 01:16 AM

    Hi Mohan,
    if you want to use a really flexible and Open Solution you should take a look on noxDB from Niels Liisberg.
    https://github.com/sitemule/noxDB

    It is open source and has many features to create, manipulate and read Json as well as Xml Documents and is very easy to use within RPG.
    It is also very fast because it is all written in ILE C and ILE RPG. 
    The IBM SQL Functions also work fine but my experience in the past projects is, that they have some performance issues when it comes to larger documents and many requests. They also need much more RAM.

    If you have any questions about that, send them to me and have fun and success while trying noxDB.

    Greetings
    Markus



    ------------------------------
    Markus A. Litters
    ------------------------------



  • 14.  RE: How can I employ RPGLE to generate a JSON document in IFS

    Posted Tue April 11, 2023 09:30 AM

    Both Scott's YAJL and noxDB work nicely. 

    I have not worked with the DB2 functions for this, but You will mst likely find YAJL or noxDB do the job perfectly.

    They may or may not be compatible with V7R1.




    ------------------------------
    Richard Schoen
    ------------------------------



  • 15.  RE: How can I employ RPGLE to generate a JSON document in IFS
    Best Answer

    Posted Tue April 11, 2023 07:57 PM
    Edited by Mohan Eashver Wed April 12, 2023 07:11 AM

    Dear Richard

    >>>>  They may or may not be compatible with V7R1. <<<<

    In the web page of Scott Klement's YAJL package download, he indicates clearly which lowest IBM i releases the packages support. Many of them support IBM i 6.1 as the lowest release. 





    ------------------------------
    Education is not the learning of facts but the training of the mind to think. -- Albert Einstein.
    ------------------------------
    Satid S.
    ------------------------------



  • 16.  RE: How can I employ RPGLE to generate a JSON document in IFS

    Posted Wed April 12, 2023 07:24 AM

    Hello Satid,

    Thank you very much for pointing me to Scott's links. 
    Saves me a lot of research.
    You are a gift to the IBM i community.



    ------------------------------
    Mohan Eashver
    ------------------------------



  • 17.  RE: How can I employ RPGLE to generate a JSON document in IFS

    Posted Wed April 12, 2023 07:26 AM

    Thank you Richard.



    ------------------------------
    Mohan Eashver
    ------------------------------