Modern mainframe systems
call REST APIs
process Kafka messages
receive JSON payloads
communicate mobil backends
do cloud integrations
The modern JSON communication works in UTF-8 character coding with some rare exceptions.
The JSON PARSE
reads a JSON text
ignores whitespaces, line breaks, indentation (handles formatted and unformatted JSONs)
separates attributes to fields
fills COBOL structures
sends back error codes
converts numbers
trims quote marks
handles nested objects
handles arrays
The INTO clause contains the targeted COBOL structure.
The WITH DETAIL will give back detailed error description in case an error is raised (at which field, on which character position, which character conversion failed).
The VALIDATE checks the validity of the JSON text for syntax errors. See example below.
The SUPPRESS leaves fields out from parsing the corresponding JSON attributes.
The CONVERTING clause tranforms the strings under another character coding systems:
DISPLAY (UTF-8 to EBCDIC)
NATIONAL (UTF-8 to UTF-16)
The ON EXCEPTION receives the flow in case the parsing had an error.
In the case the JSON does not contain all matching fields in the structure, those fields will not change their original value. E.g. if the structure has a CITY PIC X(50) field which contains "NW" value, but the JSON has no such attribute, or it was suppressed, the CITY field will keep the value "NW".
In the case when the JSON has attributes not matching to any fields, those attributes will be ignored, using SUPPRESS on those attributes is not necessary.
In case the attribute value size is larger than the matching field has, the rest of the size will be trimmed, and no error will be raised.
In the case the array in the JSON has more elements than the structure in COBOL has, the parser will trim the JSON array without raising error.
In case the array in the COBOL structure has more elements than the JSON array has, only the matching OCCURS elements will be filled by the parser, the rest will be left as they were, and no error will be raised.
The JSON validation is always important step before parsing. The VALIDATE clause forces the parser to go through the whole JSON. If any error is found, steps into the ON EXCEPTION branch while if the WITH DETAIL was given as well, will provide detailed description of the found error. If no error is found, the parser will pass filling the structure, it stays as it was.
The JSON PARSE case-sensitive for JSON attribute names and field names. They must match from character to character or the parser will not pair them.
EXAMPLE FOR SYNTAX OF JSON PARSE:
EXAMPLE FOR VALIDATING JSON:
EXAMPLE FOR PARSING SIMPLE JSON:
EXAMPLE FOR PARSING JSON WITH NESTED OBJECT:
EXAMPLE FOR PARSING JSON WITH ARRAY:
EXAMPLE FOR PARSING JSON WITH NESTED OBJECTS AND ARRAY:
The JSON GENERATE
places quotes around values
escapes characters
converts numeric fields to numbers
tranforms group fields to objects
transforms OCCURS fields to arrays
The FROM clause contains the targeted JSON text.
The WITH DETAIL will give back detailed error description in case an error is raised (at which field, on which character position, which character conversion failed).
The SUPPRESS leaves attributes out.
The ENCODING sets the JSON character coding (default is UTF-8).
The FORMATTED clause forces the generator to create a formatted, indented output.
The NO FORMATTING creates a one-liner JSON output. This is the default.
The ON EXCEPTION receives the flow in case the parsing had an error.
EXAMPLE FOR GENERATING JSON: