Appendix A: The JSON PARSE program

The following program is used in Tutorial: Parsing a JSON text string.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. JSONPARSE.

       SPECIAL-NAMES.
       CURRENCY SIGN IS '€' WITH PICTURE SYMBOL 'Q'.

       FILE-CONTROL.
           SELECT JSON-FILE ASSIGN 
	"C:\JSONPROCESSING\DATA\JSONout.DAT"
               ORGANIZATION IS SEQUENTIAL.

       DATA DIVISION.
       FILE SECTION.
       FD  JSON-FILE.
       01  JSON-RECORD.
           03  JSON-TEXT-STRING                       PIC X(650).

       WORKING-STORAGE SECTION.
       01  COBOL-STRUCTURE.
           03  firstName                              PIC X(50). 
           03  lastName                               PIC X(50). 
           03  age                                    PIC 9(04).
           03  postalAddress.
               05  street                             PIC X(50).
               05  town                               PIC X(50).
               05  county                             PIC X(50).
           03  phoneNumber
               occurs 1 to 5 times depending on num-of-phones.
               05  typ                                PIC X(06).
               05  num                                PIC 9(09).
           03  _reference                             PIC X(12).
           03  children.
               05  child                              PIC X(50)
               occurs 1 to 5 times depending on num-of-children.
           03  pets                                   PIC X(10)
               occurs 1 to 5 times depending on num-of-pets.
           03  salary                                 PIC X(7).

       01  basic-person-info.
           03  firstName                              PIC X(50).
           03  lastName                               PIC X(50).
           03  age                                    PIC 9(04).
           03  postalAddress.
               05  town                               PIC X(50).
           03  phoneNumber
               occurs 1 to 5 times depending on num-of-phones.
               05  typ                                PIC X(06).
               05  num                                PIC 9(09).
           03  _reference                             PIC X(12).
           03  pets                                   PIC X(10)
               occurs 1 to 5 times depending on num-of-pets.

       01  num-of-phones                              PIC 9(04) comp-5
                                                      value 5.
       01  num-of-children                            PIC 9(04) comp-5
                                                      value 5.
       01  num-of-pets                                PIC 9(04) comp-5
                                                      value 5.

       PROCEDURE DIVISION.
       DECLARATIVES.
       P-DECL SECTION.
      * It is valid to get a 9/12 error code 
           USE AFTER ERROR PROCEDURE ON JSON-FILE.
               CONTINUE.
       P-DECL-EXIT.
           EXIT.

       END DECLARATIVES.
       MAIN SECTION.
           OPEN INPUT JSON-FILE
           PERFORM J100-SIMPLE-PARSE
      *>   PERFORM J200-PARSE-NOT-EXCEPTION
      *>   PERFORM J300-COMPLEX-PARSE

           CLOSE JSON-FILE

           STOP "-- Press Return --"
           GOBACK
           .

       D000-DISPLAY SECTION.

       D100-DISPLAY-DATA1.
           *> DISPLAY THE FIELDS EXTRACTED FROM THE JSON STRUCTURE
           DISPLAY FIRSTNAME OF COBOL-STRUCTURE
           DISPLAY LASTNAME OF COBOL-STRUCTURE
           DISPLAY AGE OF COBOL-STRUCTURE
           EXHIBIT NAMED TOWN OF COBOL-STRUCTURE
           .

       D150-DISPLAY-DATA2.
           DISPLAY " "
           DISPLAY FIRSTNAME OF basic-person-info
           DISPLAY LASTNAME OF basic-person-info
           DISPLAY AGE OF basic-person-info
           EXHIBIT NAMED TOWN OF basic-person-info
           EXHIBIT NAMED num-of-pets
           .

       D200-DISPLAY-JSON-STATUS.
           EXHIBIT NAMED JSON-CODE
           EXHIBIT NAMED JSON-STATUS
           .
       D999-EXIT.
           EXIT
           .

       J000-JSON-PARSE SECTION.

       J100-SIMPLE-PARSE.
           READ JSON-FILE

           JSON PARSE JSON-TEXT-STRING INTO COBOL-STRUCTURE

           IF JSON-CODE = ZEROES
               PERFORM D100-DISPLAY-DATA1
           ELSE
               PERFORM D200-DISPLAY-JSON-STATUS
           END-IF
           .
       J200-PARSE-NOT-EXCEPTION.
           READ JSON-FILE

           JSON PARSE JSON-TEXT-STRING INTO basic-person-info
               WITH DETAIL
               NOT ON EXCEPTION
                   PERFORM D150-DISPLAY-DATA2
                   GO TO J999-EXIT
           END-JSON

           PERFORM D200-DISPLAY-JSON-STATUS
           .

       J300-COMPLEX-PARSE.
           READ JSON-FILE

           JSON PARSE JSON-TEXT-STRING INTO basic-person-info
               WITH DETAIL
               NAME OF basic-person-info IS 'COBOL-STRUCTURE'
                       _reference of basic-person-info IS 'reference'
               SUPPRESS lastName of basic-person-info
               ON EXCEPTION
                   PERFORM D200-DISPLAY-JSON-STATUS
                   GO TO J999-EXIT
           END-JSON

           PERFORM D150-DISPLAY-DATA2
           .

       J999-EXIT.
           EXIT
           .