Chapter 38 Quiz: Modern COBOL Integrations

Test your understanding of JSON PARSE/GENERATE, XML processing, REST APIs, SOAP, IBM MQ, z/OS Connect, and microservices integration with COBOL. Each question has one correct answer unless otherwise stated. Try to answer each question before revealing the answer.


Multiple Choice

Question 1

Which COBOL statement converts a COBOL data structure into a JSON string?

A) JSON PARSE B) JSON GENERATE C) JSON CONVERT D) JSON FORMAT

Show Answer **B) JSON GENERATE** JSON GENERATE takes a COBOL data structure (group item) and produces a JSON representation in a target alphanumeric field. JSON PARSE does the reverse -- it converts a JSON string into a COBOL data structure. JSON CONVERT and JSON FORMAT are not valid COBOL statements.

Question 2

What does the COUNT clause do in a JSON GENERATE statement?

A) Counts the number of JSON fields generated B) Limits the number of fields to include in the output C) Stores the number of characters written to the output buffer D) Counts the number of errors encountered during generation

Show Answer **C) Stores the number of characters written to the output buffer** The COUNT clause specifies a numeric data item that receives the length of the generated JSON string. This is essential because the output buffer is typically larger than the generated JSON. Without COUNT, you would not know where the valid JSON ends and the filler spaces begin. Use the count to reference only the valid portion: `WS-JSON(1:WS-JSON-LEN)`.

Question 3

Which IBM technology exposes COBOL programs running on z/OS as RESTful APIs?

A) IBM MQ B) IBM DataPower C) z/OS Connect EE D) IBM CICS Transaction Gateway

Show Answer **C) z/OS Connect EE** z/OS Connect Enterprise Edition provides the infrastructure to expose COBOL programs (in CICS, IMS, or batch) as RESTful APIs with JSON payloads. It handles the mapping between JSON and COBOL copybook structures, HTTP protocol management, and generates OpenAPI specifications. IBM MQ is for messaging, DataPower is an API gateway appliance, and CICS TG provides JCA connectivity to CICS.

Question 4

In IBM MQ, which API call establishes a connection to a queue manager?

A) MQOPEN B) MQCONN C) MQINIT D) MQBIND

Show Answer **B) MQCONN** MQCONN (or MQCONNX for extended connection) establishes the connection between a COBOL program and an MQ queue manager. It returns a connection handle (HCONN) used by all subsequent MQ API calls. MQOPEN opens a specific queue, MQINIT and MQBIND are not valid MQ API calls.

Question 5

What is the purpose of the JSON NAME clause in a COBOL data definition?

A) It assigns a variable name for use in JSON PARSE error messages B) It specifies the JSON field name to use instead of the COBOL data name C) It defines a validation rule for JSON field values D) It marks a field for inclusion in JSON output

Show Answer **B) It specifies the JSON field name to use instead of the COBOL data name** The JSON NAME clause allows you to map COBOL data names (which use hyphens and uppercase by convention) to different JSON field names (which typically use camelCase). For example: `05 WS-ACCT-NUM PIC 9(10) JSON NAME "accountNumber"`. Without this clause, the COBOL data name is used as-is in the JSON output.

Question 6

Which MQ API call sends a message to a queue?

A) MQPUT B) MQSEND C) MQWRITE D) MQPOST

Show Answer **A) MQPUT** MQPUT places a message on an open queue. It requires the connection handle, object handle (from MQOPEN), a message descriptor, put-message options, the message length, and the message buffer. MQSEND, MQWRITE, and MQPOST are not valid IBM MQ API calls.

Question 7

What is a Service Archive (SAR) file in z/OS Connect?

A) A backup of the COBOL source code B) A deployable package containing service definitions and mappings C) A compressed log of all API requests D) An archive of previous service versions

Show Answer **B) A deployable package containing service definitions and mappings** A SAR file is the deployment unit for z/OS Connect. It contains the service definition (which CICS or IMS transaction to invoke), request and response mapping rules (JSON to COBOL and back), and metadata about the service. SAR files are deployed to the z/OS Connect server to make the API available.

Question 8

Which COBOL statement is used to convert an XML document into a COBOL data structure?

A) XML GENERATE B) XML CONVERT C) XML PARSE D) XML READ

Show Answer **C) XML PARSE** XML PARSE reads an XML document and fires events (start-of-element, content, end-of-element) that a processing procedure can handle to populate COBOL data items. XML GENERATE does the reverse (COBOL to XML). XML CONVERT and XML READ are not valid COBOL statements.

Question 9

When a COBOL program calls an external REST API through z/OS Connect API Requester, what format is the outbound request typically in?

A) EBCDIC fixed-width records B) SOAP XML envelope C) JSON D) Binary COMMAREA

Show Answer **C) JSON** REST APIs conventionally use JSON for request and response bodies. The z/OS Connect API Requester converts COBOL data structures to JSON for the outbound request, makes the HTTP call, receives the JSON response, and maps it back to COBOL structures. The COBOL program works with native COBOL data items throughout; the JSON conversion is handled by z/OS Connect.

Question 10

What happens when JSON PARSE encounters a JSON field name that does not match any field in the COBOL target structure?

A) The program abends with a data exception B) The unmatched field is ignored by default C) The field value is stored in a special overflow area D) The parse fails immediately with an exception

Show Answer **B) The unmatched field is ignored by default** By default, JSON PARSE silently skips JSON fields that do not correspond to any field in the COBOL target structure. The unmatched data is simply discarded. This behavior can be modified with compiler options or the SUPPRESS phrase. Programs should validate that all expected fields were populated after parsing.

Question 11

Which protocol does SOAP use for defining service contracts?

A) OpenAPI/Swagger B) WSDL (Web Services Description Language) C) GraphQL Schema D) JSON Schema

Show Answer **B) WSDL (Web Services Description Language)** SOAP web services use WSDL to define their contracts, including the operations available, message formats, and endpoint locations. WSDL is an XML-based description language. OpenAPI/Swagger is used for REST APIs. GraphQL Schema is for GraphQL APIs. JSON Schema validates JSON documents but is not a service contract language.

Question 12

In a COBOL program using IBM MQ, what is the purpose of the Message Descriptor (MQMD)?

A) It describes the queue manager configuration B) It contains metadata about the message such as format, persistence, and message ID C) It defines the maximum message size for the queue D) It specifies the network route the message should take

Show Answer **B) It contains metadata about the message such as format, persistence, and message ID** The MQMD (Message Descriptor) structure accompanies every message and contains metadata: message format (string, binary), persistence (survives restart or not), message type (datagram, request, reply), message ID, correlation ID, reply-to queue, expiry time, and more. It is filled before MQPUT and populated by the system during MQGET.

Question 13

What is the primary advantage of using z/OS Connect over directly coding HTTP handling in CICS?

A) z/OS Connect is free while CICS web services require a license B) z/OS Connect handles JSON-to-COBOL mapping, API management, and OpenAPI generation automatically C) z/OS Connect runs faster because it bypasses CICS entirely D) z/OS Connect supports only GET requests, which are simpler

Show Answer **B) z/OS Connect handles JSON-to-COBOL mapping, API management, and OpenAPI generation automatically** z/OS Connect provides a complete API lifecycle management solution. It automatically generates JSON-to-COBOL mappings from copybooks, produces OpenAPI documentation, and integrates with API management tools for security, rate limiting, and analytics. Direct CICS HTTP coding requires manual JSON parsing, manual documentation, and does not provide management capabilities.

True/False

Question 14

True or False: JSON GENERATE can produce a JSON array from a COBOL table defined with an OCCURS clause.

Show Answer **True** JSON GENERATE maps COBOL OCCURS clauses to JSON arrays. Each occurrence of the table becomes an element in the JSON array. The array elements maintain the structure of the COBOL table entry (elementary items become JSON properties within each array element).

Question 15

True or False: IBM MQ guarantees that messages are delivered in the order they were sent, even across multiple senders.

Show Answer **False** IBM MQ guarantees message ordering only within a single sender on a single connection to a single queue. When multiple senders put messages on the same queue, the order of retrieval depends on message priority, persistence settings, and timing. Messages from different senders may interleave. Applications that require strict global ordering must implement sequencing logic at the application level.

Question 16

True or False: A COBOL program exposed as a REST API through z/OS Connect must be modified to handle HTTP directly.

Show Answer **False** The COBOL program does not need any modification for HTTP handling. z/OS Connect acts as an intermediary that accepts HTTP requests, maps JSON to COBOL data structures, invokes the COBOL program through normal CICS or IMS mechanisms (COMMAREA, message), and maps the COBOL response back to JSON. The COBOL program processes its standard data structures, completely unaware of HTTP or JSON.

Question 17

True or False: XML PARSE in COBOL uses an event-driven (SAX-like) processing model.

Show Answer **True** XML PARSE in COBOL uses an event-driven model similar to SAX parsing. As the parser processes the XML document, it fires events (start-of-element, content, end-of-element, exception) and invokes a processing procedure that the program provides. The processing procedure examines the current XML event and XML text to extract data into COBOL variables. This is different from a DOM-based approach that loads the entire document into memory.

Question 18

True or False: MQGET with the MQGMO-WAIT option causes the COBOL program to wait indefinitely for a message.

Show Answer **False** MQGMO-WAIT causes the COBOL program to wait, but the wait duration is controlled by the MQGMO-WAITINTERVAL field in the get-message options structure. This can be set to a specific number of milliseconds (e.g., 5000 for 5 seconds) or to MQWI-UNLIMITED for an indefinite wait. Without explicitly setting WAITINTERVAL to MQWI-UNLIMITED, the wait has a finite duration.

Question 19

True or False: JSON GENERATE and JSON PARSE are available in all COBOL compilers.

Show Answer **False** JSON GENERATE and JSON PARSE are features introduced in IBM Enterprise COBOL V6.1 and later versions. They are not available in older COBOL compilers or in all vendor implementations. GnuCOBOL, for example, does not support these statements natively. Programs targeting older compilers must use string manipulation or external utility programs for JSON processing.

Question 20

True or False: REST APIs can only return JSON responses.

Show Answer **False** REST APIs can return responses in any format. While JSON is the most common format for modern REST APIs, they can also return XML, plain text, HTML, CSV, binary data, or any other content type. The response format is indicated by the Content-Type HTTP header. z/OS Connect primarily uses JSON, but the REST architectural style itself does not mandate a specific data format.

Question 21

True or False: The SUPPRESS clause in JSON GENERATE can exclude specific fields from the JSON output.

Show Answer **True** The SUPPRESS clause allows you to selectively exclude fields from the generated JSON output. You can suppress fields that contain spaces, zeros, or specific values. For example: `JSON GENERATE ... SUPPRESS WS-OPTIONAL-FIELD WHEN SPACES`. This is useful for producing cleaner JSON that omits empty or default-value fields.

Code Analysis

Question 22

What is wrong with the following JSON GENERATE code?

       01  WS-JSON-OUT        PIC X(50).
       01  WS-JSON-LEN        PIC 9(3).
       01  WS-DATA.
           05  ACCT-NUM       PIC 9(10) VALUE 1234567890.
           05  ACCT-NAME      PIC X(40)
                               VALUE "CORPORATE TREASURY ACCOUNT".
           05  BALANCE         PIC 9(9)V99 VALUE 1500000.00.

           JSON GENERATE WS-JSON-OUT FROM WS-DATA
               COUNT WS-JSON-LEN
Show Answer The output buffer `WS-JSON-OUT` is too small at **50 bytes**. The JSON generated from this structure will include field names and values that easily exceed 50 characters. The account name alone is 40 characters, and with JSON syntax (braces, quotes, colons, commas) plus the other fields, the total will be well over 100 characters. This will cause a truncation error or exception. The fix is to increase the buffer size:
       01  WS-JSON-OUT        PIC X(500).
Additionally, `WS-JSON-LEN` at PIC 9(3) can only hold up to 999, which may be sufficient for this data but is risky for larger structures. Consider PIC 9(5) for safety.

Question 23

Analyze this MQ code and identify the error:

           CALL 'MQCONN' USING WS-QM-NAME
                                WS-HCONN
                                WS-COMP-CODE
                                WS-REASON

           MOVE MQOO-INPUT-AS-Q-DEF TO WS-OPEN-OPTIONS
           CALL 'MQOPEN' USING WS-HCONN
                                WS-OBJDESC
                                WS-OPEN-OPTIONS
                                WS-HOBJ
                                WS-COMP-CODE
                                WS-REASON

           CALL 'MQPUT'  USING WS-HCONN
                                WS-HOBJ
                                WS-MSGDESC
                                WS-PMO
                                WS-MSG-LENGTH
                                WS-MSG-BUFFER
                                WS-COMP-CODE
                                WS-REASON
Show Answer The queue is opened with **MQOO-INPUT-AS-Q-DEF**, which opens the queue for **input (reading messages)**. But the code then attempts **MQPUT** (sending a message), which requires the queue to be opened with **MQOO-OUTPUT**. The MQPUT call will fail with reason code **MQRC-NOT-OPEN-FOR-OUTPUT (2039)**. The fix is either: - Change the open option to `MQOO-OUTPUT` if the intent is to send messages, or - Change the MQPUT to `MQGET` if the intent is to receive messages.

Question 24

What is the output of this JSON GENERATE with nested structures?

       01  WS-RESPONSE.
           05  STATUS-CODE    PIC 9(3) VALUE 200.
           05  STATUS-MSG     PIC X(10) VALUE "OK".
           05  RESULT-DATA.
               10  ITEM-COUNT PIC 9(3) VALUE 5.
               10  TOTAL-AMT  PIC 9(7)V99 VALUE 1250.75.

       01  WS-JSON            PIC X(300).
       01  WS-LEN             PIC 9(5).

           JSON GENERATE WS-JSON FROM WS-RESPONSE
               COUNT WS-LEN
           DISPLAY WS-JSON(1:WS-LEN)
Show Answer The output is (approximately, formatting may vary by compiler):
{"STATUS-CODE":200,"STATUS-MSG":"OK","RESULT-DATA":{"ITEM-COUNT":5,"TOTAL-AMT":1250.75}}
Key observations: - `STATUS-CODE` and `ITEM-COUNT` are rendered as JSON numbers (no quotes) because they are PIC 9 (numeric). - `STATUS-MSG` is rendered as a JSON string (with quotes) because it is PIC X (alphanumeric). - `RESULT-DATA` is a group item, so it becomes a nested JSON object with its own braces. - `TOTAL-AMT` is rendered as a decimal number 1250.75 (the implied decimal V is respected in JSON output). - Leading zeros in numeric fields are suppressed in JSON output.

Question 25

A developer writes the following code to parse a JSON response but gets unexpected results. Explain why:

       01  WS-JSON-INPUT      PIC X(200) VALUE
           '{"firstName":"Elena","lastName":"Martinez"}'.

       01  WS-PERSON.
           05  FIRST-NAME     PIC X(20).
           05  LAST-NAME      PIC X(25).

           JSON PARSE WS-JSON-INPUT INTO WS-PERSON

After parsing, FIRST-NAME and LAST-NAME are both spaces.

Show Answer The JSON field names (`firstName`, `lastName`) do not match the COBOL data names (`FIRST-NAME`, `LAST-NAME`). JSON PARSE matches JSON field names to COBOL data names, and the matching is typically **case-insensitive** but requires the names to correspond (hyphens vs. camelCase are not automatically mapped). `firstName` does not match `FIRST-NAME` because the hyphen and the concatenation are different. The JSON parser treats `firstName` as an unmatched field and ignores it. The fix is to use **JSON NAME** clauses:
       01  WS-PERSON.
           05  FIRST-NAME     PIC X(20)
               JSON NAME "firstName".
           05  LAST-NAME      PIC X(25)
               JSON NAME "lastName".
Alternatively, change the JSON field names to match the COBOL names exactly (e.g., `"FIRST-NAME"`).

Question 26

Review the following error handling for MQ operations and identify what is missing:

       1000-SEND-MESSAGE.
           CALL 'MQCONN' USING WS-QM-NAME
                                WS-HCONN
                                WS-COMP-CODE
                                WS-REASON
           IF WS-COMP-CODE = MQCC-OK
               CALL 'MQOPEN' USING WS-HCONN
                                    WS-OBJDESC
                                    WS-OPEN-OPTIONS
                                    WS-HOBJ
                                    WS-COMP-CODE
                                    WS-REASON
           END-IF
           IF WS-COMP-CODE = MQCC-OK
               CALL 'MQPUT' USING WS-HCONN
                                   WS-HOBJ
                                   WS-MSGDESC
                                   WS-PMO
                                   WS-MSG-LENGTH
                                   WS-MSG-BUFFER
                                   WS-COMP-CODE
                                   WS-REASON
           END-IF
           .
Show Answer The code is missing **resource cleanup** (MQCLOSE and MQDISC). Several issues: 1. **No MQCLOSE**: The queue handle (`WS-HOBJ`) is never closed. This leaks resources on the queue manager and may prevent other programs from accessing the queue exclusively. 2. **No MQDISC**: The connection to the queue manager (`WS-HCONN`) is never disconnected. This leaks connection resources and may hit the queue manager's connection limit. 3. **No error-path cleanup**: If MQOPEN fails, the program should still call MQDISC to clean up the connection. If MQPUT fails, the program should call both MQCLOSE and MQDISC. 4. **No MQCC-WARNING handling**: The code only checks for MQCC-OK. MQCC-WARNING (partial success) is a valid completion code that may require special handling (e.g., message truncation on MQGET). 5. **No logging of reason codes**: When errors occur, the reason code is not displayed or logged, making diagnosis difficult.

Question 27

What will happen when this XML PARSE code executes with the given XML input?

       01  WS-XML-INPUT       PIC X(200) VALUE
           '<employee><name>Smith</name>'
         & '<salary>75000</salary></employee>'.

       01  WS-EMP.
           05  WS-NAME        PIC X(20).
           05  WS-SALARY      PIC 9(7).

           XML PARSE WS-XML-INPUT
               PROCESSING PROCEDURE 8000-XML-HANDLER

And the processing procedure:

       8000-XML-HANDLER.
           EVALUATE XML-EVENT
               WHEN "START-OF-ELEMENT"
                   CONTINUE
               WHEN "CONTENT-CHARACTERS"
                   EVALUATE XML-TEXT
                       WHEN "Smith"
                           MOVE XML-TEXT TO WS-NAME
                   END-EVALUATE
           END-EVALUATE
           .
Show Answer The code will successfully parse `WS-NAME` as "Smith" but will **not capture the salary**. The processing procedure only handles the case where `XML-TEXT` equals "Smith" -- it does not have any logic to capture the salary value "75000". Additionally, the approach of matching on content values is fragile. A better approach is to track which element is currently being parsed and assign the content accordingly:
       8000-XML-HANDLER.
           EVALUATE XML-EVENT
               WHEN "START-OF-ELEMENT"
                   MOVE XML-TEXT TO WS-CURRENT-ELEMENT
               WHEN "CONTENT-CHARACTERS"
                   EVALUATE WS-CURRENT-ELEMENT
                       WHEN "name"
                           MOVE XML-TEXT TO WS-NAME
                       WHEN "salary"
                           MOVE XML-TEXT TO WS-SALARY
                   END-EVALUATE
           END-EVALUATE
           .
This approach uses the element name to determine where to store the content, which is the standard XML PARSE processing pattern.

Question 28

A COBOL program generates JSON for a financial API. The requirement states that monetary amounts must appear as strings with exactly two decimal places (e.g., "1500.00") rather than as JSON numbers (e.g., 1500). But JSON GENERATE produces numbers for PIC 9 fields. How would you solve this?

Show Answer JSON GENERATE renders PIC 9 (numeric) fields as JSON numbers, which may drop trailing zeros (e.g., 1500.00 becomes 1500). To force string representation with fixed decimal formatting, use one of these approaches: **Approach 1: Pre-format as alphanumeric** Define a separate output structure with the monetary fields as PIC X, pre-formatted:
       01  WS-OUTPUT.
           05  AMOUNT-DISPLAY PIC X(12)
               JSON NAME "amount".

       MOVE WS-AMOUNT TO WS-FMT-AMOUNT
       MOVE WS-FMT-AMOUNT TO AMOUNT-DISPLAY
This produces `"amount":" 1500.00"` (a string). **Approach 2: Use edited PIC for display** Move the numeric value to an edited PIC field, then move that to a PIC X field used in JSON generation. **Approach 3: Manual JSON construction** For critical formatting requirements, construct the JSON string manually using STRING, which gives full control over the output format. This avoids relying on compiler-specific JSON GENERATE behavior for formatting details. The best practice for financial APIs is Approach 1 or 3, as they provide deterministic formatting regardless of compiler implementation.

Scoring Guide

Score Level
25-28 Expert -- You have mastered modern COBOL integration
20-24 Proficient -- Strong understanding with minor gaps
15-19 Intermediate -- Good foundation, review API and MQ topics
10-14 Developing -- Review JSON/XML handling and integration patterns
Below 10 Beginning -- Revisit the chapter material before proceeding