Skip to main content

Import COBOL copybooks

A COBOL copybook is the record layout of a mainframe file, message or CICS COMMAREA: level numbers give the nesting, PIC clauses give each item its type and width, OCCURS makes a table and REDEFINES lays the same bytes out a second way. The COBOL Copybook importer (cobolcopybook) reads a copybook into the Catalog as a data schema. You can then search, version, diff and lint it like any other catalog item. Each version compares the records by byte position, which is how the programs that read them see them.

What you can import​

A copybook with one 01 record, in fixed format (sequence area in columns 1–6, code from column 8) or free format:

01 PAYMENT-RECORD.
05 PAYMENT-ID PIC 9(10).
05 PAYMENT-TYPE PIC X(1).
88 PAY-BY-CARD VALUE 'C'.
88 PAY-BY-BANK VALUE 'B'.
05 PAYMENT-AMOUNT PIC S9(9)V99 COMP-3.
05 PAYMENT-DETAIL PIC X(30).
05 CARD-DETAIL REDEFINES PAYMENT-DETAIL.
10 CARD-NUMBER PIC 9(16).
10 CARD-EXPIRY-YYMM PIC 9(4).
10 FILLER PIC X(10).
05 BANK-DETAIL REDEFINES PAYMENT-DETAIL.
10 BANK-ROUTING PIC 9(9).
10 BANK-ACCOUNT PIC X(17).
10 FILLER PIC X(4).
05 PAYMENT-POSTED-DATE PIC 9(8).

The importer reads PICTURE, USAGE (DISPLAY, COMP / BINARY, COMP-3 / PACKED-DECIMAL, COMP-1, COMP-2), fixed and variable OCCURS … DEPENDING ON, REDEFINES and 88-level conditions. It also reads clauses continued onto the next line.

Import it​

  1. Go to Bring in → Catalog and click "Import to catalog" (see the import wizard).
  2. On Source, choose File and drop the copybook — try apiome-ui/examples/cobol-copybook/03-payment-redefines.cpy — or choose Paste and paste it.
  3. On Detect & route, Apiome reports Auto-detected: COBOL Copybook (cobolcopybook) and routes it to the Catalog.
  4. On Quality, review the lint findings and click "Import".

Over the API, send the copybook to the import endpoints with the source kind cobolcopybook; see Import sources. From the CLI:

apiome import cobolcopybook ./03-payment-redefines.cpy

The eight examples in apiome-ui/examples/cobol-copybook/ include a customer record with a variable phone table, an order line, a payment with two REDEFINES layouts, a warehouse snapshot with packed and binary items, an ACH entry and an account master. One has unmodelled clauses and one has deliberately faulty overlays.

What Apiome reads​

In the copybookBecomes
The 01 record and each group itemA record type (PAYMENT-RECORD.CARD-DETAIL), one field per subordinate item, in source order
PIC X(n), PIC A(n), a numeric-edited picture (ZZ9.99)string, its width as the maximum length
PIC 9(n) / S9(n), up to 9 digits / up to 18i32 / i64, with the bounds the digits allow (minimum 0 when unsigned)
A picture with an implied decimal point (V), or more than 18 digitsdecimal, with its bounds
COMP-1 / COMP-2float / double
OCCURS n TIMES, OCCURS m TO n TIMES DEPENDING ONA list of the item's type, its bounds as the minimum and maximum item count
REDEFINESA union named <base>-OVERLAY with one member per layout of the shared bytes (see below)
Each item's positionIts byte offset and length in the record, and how its bytes are encoded (below)

Every field also keeps its level number, PIC and USAGE text, OCCURS bounds, DEPENDING ON controller and 88-level conditions. The item's Format details tab shows the full layout as a storage map; see the COBOL copybook inspector.

REDEFINES becomes a union​

CARD-DETAIL and BANK-DETAIL are not more fields after PAYMENT-DETAIL. They are the same 30 bytes, read two other ways. The record therefore keeps one field, PAYMENT-DETAIL, typed by the union PAYMENT-DETAIL-OVERLAY. The union's members are PAYMENT-DETAIL (the bytes as PIC X(30)), CARD-DETAIL and BANK-DETAIL. An item that redefines something the copybook does not declare stays an ordinary field, and lint flags it.

Storage and EBCDIC notes​

A copybook does not say which code page its data is in. Apiome assumes EBCDIC and records that as an assumption, together with the sizing assumptions every byte count rests on. Each elementary item records how its bytes are encoded, so a consumer moving the data off the mainframe knows what to transcode and what to leave alone:

StorageItemsWhat to do with the bytes
EBCDIC textPIC X, PIC ATranscode as text, one character per byte
EBCDIC editedNumeric-edited picturesTranscode as text; it is formatted for printing
Zoned decimalPIC 9 / S9, DISPLAYOne EBCDIC digit per byte, the sign in the last byte's zone nibble. Convert as a number
Packed decimalCOMP-3 / PACKED-DECIMALTwo digits per byte and a sign nibble. Never transcode
BinaryCOMP / COMP-4 / BINARYBig-endian two's complement. Never transcode
Hexadecimal floatCOMP-1 / COMP-2IBM hexadecimal floating point unless compiled for IEEE. Never transcode

The layout fingerprint​

Besides the fingerprint every catalog item has, a copybook item records a record-layout hash (cobol-record-layout-sha256). It is computed from each item's position, length, storage, OCCURS bounds and REDEFINES target, and never from a name. Renaming items, renumbering levels or editing 88-level conditions keeps the hash. Moving a byte changes it. Two copybooks with the same layout hash read each other's records.

Versions and changes​

Import a newer copybook into the same item to add a version. A program reads an item by its offset and length, not by its name, so the comparison grades every change by what it does to the bytes:

ChangeSeverityRule
An item starts at another offsetBreaking: it, and everything after it, is cut wronglycobolcopybook.offset-moved
An item's length, OCCURS bounds, PIC or USAGE changedBreakingcobolcopybook.length-changed, cobolcopybook.occurs-changed, cobolcopybook.picture-changed
An item removed, or inserted before other itemsBreakingcobolcopybook.field-removed, cobolcopybook.field-inserted
An item added after the last byteDangerous: offsets hold, but the record growscobolcopybook.field-appended
An item added in spare FILLER, the record length unchangedSafecobolcopybook.field-added-in-place, cobolcopybook.filler-reclaimed
A REDEFINES layout added / removedDangerous / Breakingcobolcopybook.overlay-added, cobolcopybook.overlay-removed
The record grew with every offset held / changed length otherwiseDangerous / Breakingcobolcopybook.record-length-changed
An 88-level condition removed or its value changedDangerous: programs testing it breakcobolcopybook.condition-removed
Only 88-level conditions addedSafecobolcopybook.condition-added
Only level numbers changedSafecobolcopybook.level-renumbered

To see the classic safe change, import POLICY-RECORD.v1.cpy and then POLICY-RECORD.v2-compatible.cpy from apiome-rest/tests/fixtures/cobolcopybook_versions/: POL-AGENT-CODE is carved out of the spare FILLER, and every change grades Safe. POLICY-RECORD.v3-breaking.cpy inserts an item after POL-NUMBER, and every later item reports Breaking.

Lint​

The Quality step and the item's Lint & score tab add the cobolcopybook rules on top of the common ones. Each finding names the item by its qualified name (PAYMENT-RECORD.CARD-DETAIL):

RuleSeverityFlags
cobolcopybook.redefines-target-declaredWarningA REDEFINES naming no earlier item of the same group
cobolcopybook.redefines-fitsWarningA redefining layout larger than the item it redefines
cobolcopybook.redefines-discriminatedInfoAn ambiguous overlay: no earlier item has enough 88-level conditions to say which layout holds data
cobolcopybook.redefines-variableWarningAn OCCURS … DEPENDING ON table inside a REDEFINES
cobolcopybook.odo-controller-declaredWarningA DEPENDING ON naming an item the copybook does not declare
cobolcopybook.odo-controller-precedesWarningA DEPENDING ON controller declared after the table or inside it
cobolcopybook.odo-trailing-itemsInfoItems after a variable-length table, whose offsets then vary
cobolcopybook.duplicate-nameWarningA data name other than FILLER declared twice
cobolcopybook.storage-sizedWarningAn item whose storage cannot be computed, such as PIC N
cobolcopybook.packed-odd-digitsInfoA COMP-3 item with an even number of digits
cobolcopybook.filler-unusedInfoA FILLER: storage no program can read by name
cobolcopybook.condition-values-uniqueWarningTwo 88-level conditions of one item testing the same value

Import 08-overlay-warnings.cpy to see the three REDEFINES rules fire.

Export​

A copybook item writes back out as the same copybook through the COBOL Copybook target, REDEFINES and all; see Export a spec. Export it to JSON Schema to describe the record to a modern consumer: each overlay becomes a oneOf of its layouts, and each item keeps its maximum length or numeric bounds.

When an import is refused​

What Apiome reportsWhyWhat to do
Content does not appear to be a COBOL copybookNo 01 level with a PIC clauseCheck that the file is a copybook, or pick the right format
COBOL copybook must begin with a level-01 group itemA 05 (or other level) comes before the first 01Start the record at 01
Invalid or empty COBOL copybook contentThe file is empty or holds only commentsUpload the copybook itself

Level-66 RENAMES and COPY … REPLACING are not read. The record imports, and the inspector marks it partial with the reason.