The following program is used in Tutorial: Generating JSON output.
IDENTIFICATION DIVISION. PROGRAM-ID. JSONGENR8. SPECIAL-NAMES. CURRENCY SIGN IS '€' WITH PICTURE SYMBOL 'Q'. INPUT-OUTPUT SECTION. 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 Q99,999. 01 num-of-phones PIC 9(04) comp-5 value 0. 01 num-of-children PIC 9(04) comp-5 value 0. 01 num-of-pets PIC 9(04) comp-5 value 0. 01 json-doc pic X(650). 01 c1 pic 9(04) comp-5. PROCEDURE DIVISION. MAIN SECTION. OPEN OUTPUT JSON-FILE PERFORM P000-POPULATE-STRUCTURE * PERFORM J100-SIMPLE-GENERATE * PERFORM J200-GENERATE-EXCEPTION * PERFORM J300-COMPLEX-GENERATE PERFORM J400-COMPLEX-GENERATE CLOSE JSON-FILE GOBACK . P000-POPULATE-STRUCTURE SECTION. P100-MOVE-DATA. *> POPULATE THE COBOL DATA STRUCTURE MOVE 'Justin' TO FIRSTNAME MOVE 'Case' TO LASTNAME MOVE 24 TO AGE MOVE '221B Baker Street' TO STREET MOVE 'Westminster' TO TOWN MOVE 'London' TO COUNTY MOVE 2 TO NUM-OF-PHONES MOVE 'Work' TO TYP(1) MOVE 99972301212 TO NUM(1) MOVE 'Home' TO TYP(2) MOVE 99972243688 TO NUM(2) MOVE 'NS9999' TO _reference MOVE 0 TO NUM-OF-CHILDREN MOVE 3 TO NUM-OF-PETS MOVE 'Cat' TO PETS(1) MOVE 'Dog' TO PETS(2) MOVE 'Canary' TO PETS(3) MOVE 40000 TO SALARY . P999-EXIT. EXIT . J000-JSON-GENERATE SECTION. J100-SIMPLE-GENERATE. *> SIMPLIFIED GENERATE THE JSON TEXT STRUCTURE JSON GENERATE JSON-DOC FROM COBOL-STRUCTURE COUNT IN C1 IF JSON-CODE = ZEROES MOVE JSON-DOC(1:C1) TO JSON-TEXT-STRING WRITE JSON-RECORD ELSE EXHIBIT NAMED JSON-CODE EXHIBIT NAMED JSON-STATUS END-IF GO TO J999-EXIT . J200-GENERATE-EXCEPTION. *> SIMPLE GENERATE WITH ON EXCEPTION JSON GENERATE JSON-DOC FROM COBOL-STRUCTURE COUNT IN C1 ON EXCEPTION EXHIBIT NAMED JSON-CODE EXHIBIT NAMED JSON-STATUS GO TO J999-EXIT END-JSON MOVE JSON-DOC(1:C1) TO JSON-TEXT-STRING WRITE JSON-RECORD GO TO J999-EXIT . J300-COMPLEX-GENERATE. *> COMPLEX GENERATE EXAMPLE JSON GENERATE JSON-DOC FROM COBOL-STRUCTURE COUNT IN C1 NAME OF _reference OF COBOL-STRUCTURE IS 'reference' SUPPRESS salary OF COBOL-STRUCTURE typ OF COBOL-STRUCTURE ON EXCEPTION EXHIBIT NAMED JSON-CODE EXHIBIT NAMED JSON-STATUS GO TO J999-EXIT END-JSON MOVE JSON-DOC(1:C1) TO JSON-TEXT-STRING WRITE JSON-RECORD GO TO J999-EXIT . J400-COMPLEX-GENERATE. *> COMPLEX GENERATE EXAMPLE JSON GENERATE JSON-DOC FROM COBOL-STRUCTURE COUNT IN C1 NAME OF salary of COBOL-STRUCTURE IS 'Benjamins' SUPPRESS TYP OF COBOL-STRUCTURE ON EXCEPTION EXHIBIT NAMED JSON-CODE EXHIBIT NAMED JSON-STATUS GO TO J999-EXIT END-JSON MOVE JSON-DOC(1:C1) TO JSON-TEXT-STRING WRITE JSON-RECORD GO TO J999-EXIT . J999-EXIT. EXIT .