COBOL

COBOL

COBOL

COBOL is responsible for the efficient, reliable, secure, and unseen day-to-day operations of the world's economy.


#Programminglanguages
#IBMZandLinuxOne
 View Only
  • 1.  Using JSON PARSE for json that has sections

    Posted 6 days ago
      |   view attached

    I would like to parse json such as this example, where there is a client section, then a section for transactions:

    {
      "CLIENT": {
        "CLIENTID": 1047,
        "CLIENTNAME": "Alice"
      },
      "TRANSACTIONS": [
        {"DATE": "2026-10-01","AMOUNT": 100.50},
        {"DATE": "2026-10-02","AMOUNT": -20.00}
      ]
    }

    So I have a matching working storage area like this:

           01  CLIENT-DATA.
              03 CLIENT.
                 05 CLIENTID      PIC 9(9).
                 05 CLIENTNAME    PIC X(50).
              03 TRANSACTIONS OCCURS 2 TIMES.
                 05 TRANDATE      PIC X(10).
                 05 TRANAMOUNT    PIC 9(5).
    Then, after converting to UTF8, I'm trying to parse into CLIENT-DATA like so:
               JSON PARSE JSON-UTF8 INTO CLIENT-DATA
                    NAME OF TRANDATE IS "DATE"
                            TRANAMOUNT IS "AMOUNT"
              END-JSON.
    But when I run the program, I get lots of errors like this:
     IGZ0322I During execution of the JSON PARSE statement on line 37 of program JP_EX, no data item matched JSON name "CLIENT" at offset 3.
     IGZ0322I During execution of the JSON PARSE statement on line 37 of program JP_EX, no data item matched JSON name "CLIENTID" at offset 14.
    The only way I have found to get this working is to split the json into two separate blocks, then do two separate JSON PARSES, and this works but I really want to be able to parse the whole json without having to do that. I have attached the full code as an attachment. 
    Can anyone advise please?



    ------------------------------
    Steve Newbery
    ------------------------------

    Attachment(s)

    txt
    JSON PARSE code.txt   2 KB 1 version


  • 2.  RE: Using JSON PARSE for json that has sections

    Posted 5 days ago

    Hello Steve

    I think you should add CLIENT-DATA IS OMITTED in the list of NAME. 
    By the way testing the hexadecimal value of the first character of the json you receive could give the ccsid value to use in the json parse using ENCODING. This to avoid translating the json to UTF-8 before parsing. 

    Hoping it helps

    /Loic Vital-Durand



    ------------------------------
    Loic Vital-Durand
    ------------------------------



  • 3.  RE: Using JSON PARSE for json that has sections

    Posted 5 days ago
    Edited by Steve Newbery 5 days ago

    Hi Lioc, thanks for the reply, and yes that works great! 

    So now I have:

               JSON PARSE JSON-UTF8 INTO CLIENT-DATA
                  WITH DETAIL
                    NAME OF CLIENT-DATA IS OMITTED
                            TRANDATE IS "DATE"
                            TRANAMOUNT IS "AMOUNT"

    Also thanks for the tip about checking if the UTF8 conversion is necessary.



    ------------------------------
    Steve Newbery
    ------------------------------



  • 4.  RE: Using JSON PARSE for json that has sections

    Posted 5 days ago

    Why you are converting to UTF-8?

    Looks like you already have the JSON in EBCDIC, just parse on with "ENCODING 1047".



    ------------------------------
    Sergio Samayoa
    ------------------------------



  • 5.  RE: Using JSON PARSE for json that has sections

    Posted 5 days ago

    Ah thanks Sergio, I didn't notice the ENCODING phrase which is quite a recent addition I think? 

    That certainly simplifies the coding :)



    ------------------------------
    Steve Newbery
    ------------------------------