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.
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.
The Variables view shows the COBOL-STRUCTURE group being populated from the contents of the JSON file.
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.
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
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'.
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
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.