Run the program and view the JSON input

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

This tutorial uses the same data as created in the JSON file when running Tutorial: Generating JSON output; either amend the SELECT statement in the JSONParse program to match the location of the JSON file, or use the contents of Appendix B: JSON input file to create a new JSON file at the location specified.

  1. Start running the project.

    By default, the program currently runs the following JSON PARSE statement:

    JSON PARSE JSON-TEXT-STRING INTO COBOL-STRUCTURE

    After the JSON file has been parsed, the following output is displayed.

  2. Click Debug program(Debug program-name, and then use the debug controls to step through the program until it executes the JSON PARSE statement shown above.

    The Variables view shows the COBOL-STRUCTURE group being populated from the contents of the JSON file.

  3. Stop the debugging session.

    Typically, there are more issues when reading JSON text strings than there are when creating them. To help diagnose those issues you can enrich the JSON PARSE call with some extra functionality, to help resolve mismatches in the COBOL structure when compared to the JSON text string.

  4. Edit JSONParse.cbl to run the J200-PARSE-NOT-EXCEPTION paragraph by uncommenting the PERFORM J200-PARSE-NOT-EXCEPTION statement, and then save and run the program. (You can comment out the PERFORM J100-SIMPLE-PARSE statement if you wish.)

    The program now runs the following JSON PARSE statement:

    JSON PARSE JSON-TEXT-STRING INTO basic-person-info
                   WITH DETAIL
                   NOT ON EXCEPTION
                       PERFORM D150-DISPLAY-DATA2
                       GO TO J999-EXIT
               END-JSON
  5. Debug and step through the program where the WITH DETAIL clause has produced notifications to indicate that there were mismatches.

    Secondly, the display outputs from the EXCEPTION show us the error codes. The '106' is a fatal conversion error and the 3 is the reason. The error was caused by the fact that the JSON string contains the top-level field called 'COBOL-structure', which does not match the field name 'basic-person-info'.

  6. Edit JSONParse.cbl to run the J300-COMPLEX-PARSE paragraph by uncommenting the PERFORM J300-COMPLEX-PARSE statement, and then save the program. (You can also comment out the PERFORM J100-SIMPLE-PARSE and J200-PARSE-NOT-EXCEPTION statements if you wish.)

    The program now runs the following JSON PARSE statement:

    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
  7. Debug and step through the program. You will find that the PARSE statement has worked and the WITH DETAIL clause has provided information about the errors.

    The array fields have been set to the maximum number, hence the PARSE informs us that there were less occurrences in the JSON string than the array is set to hold. There is also a list of the fields that were not parsed, as they were not present in the COBOL data structure. Lastly, the display statements confirm the contents of the fields, and the lastName field was correctly suppressed.

    This concludes the tutorial.