Run the program and view the JSON output

You will run the program a number of times, using different declarations of the JSON GENERATE statement - the declarations are in the J100-SIMPLE-GENERATE, J200-GENERATE-EXCEPTION, and J300-COMPLEX-GENERATE paragraphs.

  1. If required, update the SELECT statement to change the path in which the program will write the output file; this must be an existing location. By default, JSONGenr8.cbl is set to write out to a file located at C:\JSONPROCESSING\DATA, as can be seen in the following code excerpt:

  2. Build and run the project again. You can press F11 (Step Into) to start debugging, and step through the application.

    The program currently runs the following JSON GENERATE statement:

    JSON GENERATE JSON-DOC FROM COBOL-STRUCTURE
                   COUNT IN C1
  3. Navigate to the specified path for the output file (JSONout.DAT), and then open it in a text editor.
  4. Copy the contents of the file, navigate to https://jsonformatter.org/, paste the contents in the left-hand pane, and then click Validate. (Note: you can use any available online JSON formatter.)

    The results displayed in the right-hand pane, showing that valid JSON was generated.

    We'll now update the program to enhance the JSON GENERATE statement to include some exception handling.

  5. Edit JSONGenr8.cbl to run the J200-GENERATE-EXCEPTION paragraph by uncommenting the PERFORM J200-GENERATE-EXCEPTION statement.
    Tip: Comment out the other PERFORM statements, so you only focus on the output generated from this declaration.
  6. Save, build, and run the project again.

    The program now runs the following JSON GENERATE statement:

    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

    This does not affect the generated JSON, but gives a more contemporary control over JSON GENERATE, by handling any potential exceptions within the JSON statement, instead of using a conditional statement following JSON GENERATE as shown in the J100 paragraph.

    The last declaration to run, J300, demonstrates the use of the NAME and SUPPRESS clauses. The NAME clause is used to re-map the '_reference' field in COBOL (the proceeding hyphen is due to a reserved words constraint) to 'reference' in JSON. The SUPPRESS clause is used to omit certain COBOL fields from the JSON output.

  7. Edit JSONGenr8.cbl to run the J300-COMPLEX-GENERATE paragraph by uncommenting the PERFORM J300-COMPLEX-GENERATE statement.
    Tip: Comment out the other PERFORM statements, so you only focus on the output generated from this declaration.
  8. Save, build, and run the project again.

    The program now runs the following JSON GENERATE statement:

    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
  9. Copy the contents of JSONout.json into the JSON formatter again.

    The output shows the structure using the field named 'reference', and the 'salary' and 'typ' fields have been omitted.