COBOL

COBOL

COBOL

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

 View Only
  • 1.  JSON PARSE wont parse object arrays :(

    Posted Wed March 18, 2026 08:05 PM

    I was writing a COBOL program which receive its operational parameters in JSON format but happens than when I started to test I started to get this message:

    IGZ0339W During execution of the JSON PARSE statement on line 23 of program XXXX, the value of JSON name/value pair
    with name "XXXX" at offset 999 was found to be incompatible with the matching data item.

    So I tried the example programs from here: https://www.ibm.com/docs/en/cobol-zos/6.4.0?topic=input-handling-json-arrays but they gave me the same result.

    Have you used JSON PARSE with object arrays successfully?

    Any help appreciated!



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


  • 2.  RE: JSON PARSE wont parse object arrays :(

    Posted Thu March 19, 2026 01:40 PM

    Hi Sergio, often this kind of error happens with toy programs where JSON is improperly encoded.  Here's the example program taken from the doc link in your post, with a modification where I move the JSON into json-text within the program.  I'm able to compile and run it cleanly.

           Identification division.
             Program-id. occ1.
           Data division.
            Working-storage section.
             1 some-data.
              2 msg occurs 3.
               4 ver usage comp-1.
               4 uid pic 9999 usage display.
               4 txt pic x(32).
            Linkage section.
             1 json-text pic x(128).
           Procedure division using json-text.
               move function display-of(function national-of(
                    '{"some-data":{"msg":[{"ver":5,"uid":10,"txt":"Hello"},{
          -    '"ver":5,"uid":11,"txt":"World"},{"ver":5,"uid":12,"txt":"!"}
          -    ']}}'
                    1047) 1208)
               to json-text
               Json parse json-text into some-data
                 with detail
               end-json.
               If ver(1) equal to 5 then
                 Display "Message ID is " uid(1)
                 Display "Message text is '" txt(1) "'".
               If ver(2) equal to 5 then
                 Display "Message ID is " uid(2)
                 Display "Message text is '" txt(2) "'".
               If ver(3) equal to 5 then
                 Display "Message ID is " uid(3)
                 Display "Message text is '" txt(3) "'".
               Goback.
           End program occ1.

    Note that I used 1047 as the "from" encoding for the NATIONAL-OF intrinsic because I've edited the source file using a CCSID 1047 editor.



    ------------------------------
    Jeff Shimoda
    IBM Enterprise COBOL for z/OS
    Compiler Developer
    ------------------------------



  • 3.  RE: JSON PARSE wont parse object arrays :(

    Posted Thu March 19, 2026 02:45 PM

    Hello Sergio,

    It will be useful if your code snippets that include the JSON PARSE statement can be shared.  You are parsing the JSON-TEXT in error in what encoding ? UTF-8 or what codepage of EBCDIC?
    Another useful thing to do is, using DISPLAY FUNCTION HEX-OF(<JSON-TEXT-BEING-PARSED>), check what the JSON TEXT looks like in hex values, especially around 999th byte (that is, +999 from the beginning).



    ------------------------------
    Roy Bae
    ------------------------------



  • 4.  RE: JSON PARSE wont parse object arrays :(

    Posted Fri March 20, 2026 02:56 AM

    Hello Sergio,

    As Roy mentioned it would be good to share some code to get help ;)

    Also adding some tips to what Jeffery explained :

    • find the ccsid and use encoding in the parse
      • 77 W-CCSID                     PIC 9(5).
      • EVALUATE function                                            
                         HEX-OF(theJsonToBeParsed(1:1))
         WHEN '43'             (swedish)                                      
           MOVE 278 to W-CCSID                                       
           DISPLAY 'message is EBCDIC.' W-CCSID                   
         WHEN '7B'              (utf-8)                                     
           MOVE 1208 to W-CCSID                                      
           DISPLAY 'message is UTF8.' W-CCSID                     
         WHEN OTHER                                               
            DISPLAY 'message can not be parsed unknown ccsid '
                  ' first chars are -->' function HEX-OF(theJsonToBeParsed(1:50))
               '<--'
         END-EVALUATE                                              

      • Parse the message fist without "WITH DETAIL" and if it goes wrong (ON EXCEPTION) parse it again WITH DETAIL. Doing so you will avoid having a huge sysout if it goes fine but will get all the parsing details if it goes wrong
        JSON PARSE theJsonToBeParsed             
             INTO theWorkingStoragePlace
             ENCODING W-CCSID                                        
             NAME REQUEST              IS OMITTED                    
              REQappId                IS 'appId'                     
              ....
              CONVERTING REQshowIssues FROM BOOLEAN              
                             USING REQshowIssues-flag                
                        ALSO REQFL-AM FROM BOOLEAN                   
                             USING REQFL-AM-flag                     
                .....
         ON EXCEPTION                                                
                 ADD 1 TO PARSE-KO                                         
                 DISPLAY 'Parse msg went wrong'                            
                 JSON PARSE theJsonToBeParsed             
                        INTO theWorkingStoragePlace
                        WITH DETAIL
                        ENCODING W-CCSID                                        
                         NAME REQUEST              IS OMITTED                    
                         REQappId                IS 'appId'                     
                          ....
                         CONVERTING REQshowIssues FROM BOOLEAN              
                             USING REQshowIssues-flag                
                        ALSO REQFL-AM FROM BOOLEAN                   
                             USING REQFL-AM-flag                     
                         .....
                     ON EXCEPTION                                                
                          ADD 1 TO PARSE-KO                                         
                          DISPLAY 'Parse msg went wrong'                            
                          PERFORM errorAndStop
                     NOT ON EXCEPTION                   
                         ADD 1 TO PARSE-OK               
                         DISPLAY 'Parse msg went ok'      
                END-JSON                                      
         NOT ON EXCEPTION                   
            ADD 1 TO PARSE-OK               
           DISPLAY 'Parse msg went ok'      
        END-JSON                                       

    Hope it helps

    /Loic



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



  • 5.  RE: JSON PARSE wont parse object arrays :(

    Posted Fri March 20, 2026 09:42 AM

    Hi Guys,

    This is the code I used:

    Driver program:

           IDENTIFICATION DIVISION.
           PROGRAM-ID. occ1run.

          *    cob2 occ1run.cbl occ1.cbl -o occ1run.exe

           DATA DIVISION.
           WORKING-STORAGE SECTION.

           01 j pic X(128) value
               '{"some-data":{"msg":[{"ver":5,"uid":10,"txt":"Hello"},{"ver"
          -     ':5,"uid":11,"txt":"World"},{"ver":5,"uid":12,"txt":"!"}]}}'
               .

           PROCEDURE DIVISION.
               DISPLAY "occ1run J = " J.
               CALL "OCC1" USING J.
               GOBACK.
    Program copied from IBM docs, I added the clauses "detail encoding from codepage":
            Identification division.
              Program-id. occ1.
            Data division.
             Working-storage section.
              1 some-data.
               2 msg occurs 3.
                4 ver usage comp-1.
                4 uid pic 9999 usage display.
                4 txt pic x(32).
             Linkage section.
              1 json-text pic x(128).
            Procedure division using json-text.
                Json parse json-text into some-data
                  *> added by me:
                  detail encoding from codepage
                end-json.
                If ver(1) equal to 5 then
                  Display "Message ID is " uid(1)
                  Display "Message text is '" txt(1) "'".
                If ver(2) equal to 5 then
                  Display "Message ID is " uid(2)
                  Display "Message text is '" txt(2) "'".
                If ver(3) equal to 5 then
                  Display "Message ID is " uid(3)
                  Display "Message text is '" txt(3) "'".
                Goback.
            End program occ1.
    How I compile from bash (I added the compiler options as IBM's support requested and sent to them):
    cob2 -qLIST,MAP,NOOFFSET,NOSUPPRESS \
    occ1run.cbl occ1.cbl -o occ1run.exe
    Run:
    OLS0009:/ol/ols0009/fifo: >occ1run.exe
    occ1run J = {"some-data":{"msg":[{"ver":5,"uid":10,"txt":"Hello"},{"ver":5,"uid":11,"txt":"World"},{"ver":5,"uid":12,"txt":"!"}]}}
    IGZ0339W During execution of the JSON PARSE statement on line 13 of program OCC1, the value of JSON name/value pair
             with name "msg" at offset 20 was found to be incompatible with the matching data item.
    Looks like is not a charset problem since it found "msg" property name.
    Regards!


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

    Attachment(s)

    cbl
    occ1.cbl   979 B 1 version
    cbl
    odo1.cbl   1 KB 1 version
    cbl
    odo1run.cbl   473 B 1 version
    cbl
    occ1run.cbl   473 B 1 version


  • 6.  RE: JSON PARSE wont parse object arrays :(

    Posted Fri March 20, 2026 10:02 AM

    Sergio, try using

    encoding from 1047


    ------------------------------
    Jeff Shimoda
    IBM Enterprise COBOL for z/OS
    Compiler Developer
    ------------------------------



  • 7.  RE: JSON PARSE wont parse object arrays :(

    Posted Fri March 20, 2026 10:20 AM

    Hi Guys,

    Mystery resolved, thank you for your comments, that lead me to put more attention to codepage.

    Actually was a CCSID problem, "encoding from codepage" is using compiler's codepage (1140), code points for brackets are different from 1047 so forcing "encoding 1047" the program worked as expected.

    Regards!

     



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