JSON GENERATE and JSON PARSE: Buffer Sizing, Null and Boolean Mapping, and Error Handling
In Rocket Enterprise Developer 12.0 for Linux/UNIX, COBOL applications interface with JSON APIs using native JSON GENERATE and JSON PARSE statements. JSON GENERATE transforms COBOL hierarchies into JSON text, supporting anonymous objects via OMITTED, conditional exclusion with SUPPRESS, and boolean/null generation via CONVERTING and INDICATING. Receivers must be sized 2–3 times the source record to avoid JSON-CODE 1 buffer overflows. JSON PARSE populates COBOL records from JSON text, returning execution results in JSON-CODE and non-exception warnings in JSON-STATUS. [1] [2] [3]
Read the full briefing
Scope and Architectural Overview
This article covers native JSON serialization and deserialization in Rocket Enterprise Developer / Enterprise Server 12.0 for Linux/UNIX.
Modernizing COBOL applications often requires exchanging payloads directly with RESTful web APIs and microservices. Rather than requiring external string parsers, C wrappers, or intermediate files, Rocket COBOL implements native language syntax through the The JSON GENERATE Statement and The JSON PARSE Statement.
These statements convert between hierarchical COBOL group/elementary records and structured JSON documents, enforcing strict data typing, character encodings (UTF-8, UTF-16, or EBCDIC), and special register status tracking.
JSON GENERATE: Transformation, OMITTED Objects, and Null Mapping
The JSON GENERATE statement converts COBOL data items (identifier-2) into a JSON formatted character string (identifier-1).
JSON GENERATE json-doc FROM customer-record
COUNT IN char-count
NAME OF cust-id IS "customerId"
SUPPRESS customer-notes WHEN SPACES
ON EXCEPTION
PERFORM handle-json-error
NOT ON EXCEPTION
PERFORM process-json-success
END-JSONKey operational mechanics include:
- Receiver Sizing Constraint:
identifier-1must be large enough to contain the generated JSON text including property names, quotes, colons, and braces. Rocket documentation advises sizing the receiving buffer 2 to 3 times the size of the source data item. If the receiver buffer is too small, generation fails with exception code1inJSON-CODE. - Filtering Subordinate Fields: Unnamed elementary items,
FILLERfields, slack bytes inserted forSYNCHRONIZEDitems, pointer types, and items defined withREDEFINESorRENAMESare automatically ignored during generation. - Prefix Stripping: If a COBOL data-name begins with a
#character, the#is automatically stripped from the generated JSON property name. - Anonymous Objects via
OMITTED: UsingNAME OF identifier-2 IS OMITTEDgenerates an anonymous JSON object without an outer top-level wrapper object. Similarly, applyingOMITTED(or the RocketALLkeyword) to a table generates an anonymous JSON array. - Null and Boolean Mapping: As illustrated in JSON GENERATE Examples, the
CONVERTINGphrase converts level-88 conditional flags to JSONtrue/falsebooleans, or maps figurative constants (such asSPACESorZEROES) to JSONnull. Alternatively, theINDICATINGphrase evaluates an external single-byte indicator field (or level-88 condition) to emit JSONnull.
JSON PARSE: Ingestion, Matching Rules, and Null Suppression
The JSON PARSE statement populates a COBOL target record (identifier-2) by matching name/value pairs from a JSON source string (identifier-1):
JSON PARSE json-doc INTO customer-record
WITH DETAIL
IGNORING ALL
ON EXCEPTION
DISPLAY "Parse failed with code: " JSON-CODE
END-JSONKey ingestion rules include:
- Name Matching Algorithm: Property names in the JSON document are matched to COBOL identifiers. Whitespace outside string literals is ignored.
- The
IGNORINGandINDICATINGPhrases: By default, encountering a JSONnullvalue raises warning status32in theJSON-STATUSspecial register. SpecifyingIGNORING ALLsuppresses this behavior and bypasses unhandled null fields. Alternatively,INDICATINGmoves indicator flags into specific PIC X variables to track which input attributes contained nulls. - Partial Modification Risk: If a syntax error or schema mismatch occurs midway through parsing, the statement terminates immediately, but fields parsed prior to the failure point remain populated in
identifier-2. Applications must always verify success before consuming parsed records.
Exception Codes and Special Registers
Execution outcomes are communicated through two dedicated special registers detailed in JSON-CODE Exception Codes:
JSON-CODE:0: Execution completed successfully.1: (JSON GENERATE) The receiving field was too small for the generated text. IfCOUNT INwas specified, it contains the character count actually generated before overflow.100: (JSON PARSE) Invalid JSON syntax at specified line and character offset.101: (JSON PARSE) JSON input was empty or consisted entirely of whitespace.104: (JSON PARSE) JSON value was incompatible with the matching COBOL elementary data item.105: (JSON PARSE) Unexpected JSONtrueorfalseboolean value encountered without a correspondingCONVERTINGphrase.106: (JSON PARSE) No JSON property names matched any target COBOL data item.
JSON-STATUS: Captures non-exception warning conditions during parsing, such as status code32when unhandlednullvalues are encountered.
Operational Prerequisites and Nuances
- Dynamic-Length Data Item Prohibition: Neither the JSON source/receiver text nor the COBOL record structure can be defined as a dynamic-length group or dynamic-length elementary item.
OCCURS DEPENDING ON(ODO) Interaction: If target structures use ODO tables, the controlling ODO object variable must not reside within the target record itself;JSON PARSEdoes not update the ODO object field during array ingestion.- Encoding Precedence: JSON text is assumed to be UTF-8 (CCSID 1208). If the data item is category
NATIONAL, UTF-16 (CCSID 1200) is used. When parsing EBCDIC strings, theENCODINGphrase must specify an EBCDIC CCSID orFROM CODEPAGE, which aligns with the activeMFCODESETenvironment variable.
Documentation Takeaway
In Rocket Enterprise Developer 12.0 for Linux/UNIX, JSON GENERATE and JSON PARSE eliminate external parsing libraries by transforming between COBOL structures and JSON text directly; developers must allocate receiving buffers at 2–3 times record size to prevent JSON-CODE 1 truncations, handle nulls and booleans explicitly via CONVERTING and INDICATING, and inspect JSON-CODE before trusting partially populated records on exception.