Json in COBOL

IDENTIFICATION DIVISION.
PROGRAM-ID. JSON-EXAMPLE.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT JSON-FILE ASSIGN TO "json-data.txt"
        ORGANIZATION IS LINE SEQUENTIAL.

DATA DIVISION.
FILE SECTION.
FD JSON-FILE.
01 JSON-RECORD.
    05 JSON-DATA PIC X(1000).

WORKING-STORAGE SECTION.
01 WS-JSON-DATA.
    05 WS-PAGE PIC 9(3).
    05 WS-FRUITS OCCURS 3 TIMES.
        10 WS-FRUIT PIC X(20).

01 WS-EOF PIC X VALUE 'N'.

PROCEDURE DIVISION.
MAIN-PROCEDURE.
    PERFORM ENCODE-JSON
    PERFORM DECODE-JSON
    STOP RUN.

ENCODE-JSON.
    OPEN OUTPUT JSON-FILE

    MOVE 1 TO WS-PAGE
    MOVE "apple" TO WS-FRUIT(1)
    MOVE "peach" TO WS-FRUIT(2)
    MOVE "pear" TO WS-FRUIT(3)

    STRING '{"page":' DELIMITED BY SIZE
           WS-PAGE DELIMITED BY SIZE
           ',"fruits":["' DELIMITED BY SIZE
           WS-FRUIT(1) DELIMITED BY SPACE
           '","' DELIMITED BY SIZE
           WS-FRUIT(2) DELIMITED BY SPACE
           '","' DELIMITED BY SIZE
           WS-FRUIT(3) DELIMITED BY SPACE
           '"]}' DELIMITED BY SIZE
           INTO JSON-RECORD

    WRITE JSON-RECORD
    CLOSE JSON-FILE
    DISPLAY "JSON data encoded and written to file.".

DECODE-JSON.
    OPEN INPUT JSON-FILE
    READ JSON-FILE
        AT END
            MOVE 'Y' TO WS-EOF
    END-READ

    IF WS-EOF = 'N'
        UNSTRING JSON-DATA OF JSON-RECORD DELIMITED BY ALL ','
            INTO WS-PAGE DELIMITED BY ':'
                 WS-FRUIT(1) DELIMITED BY '"'
                 WS-FRUIT(2) DELIMITED BY '"'
                 WS-FRUIT(3) DELIMITED BY '"'
        DISPLAY "Decoded JSON data:"
        DISPLAY "Page: " WS-PAGE
        DISPLAY "Fruits: " WS-FRUIT(1) ", " WS-FRUIT(2) ", " WS-FRUIT(3)
    ELSE
        DISPLAY "Error reading JSON file."
    END-IF

    CLOSE JSON-FILE.

This COBOL program demonstrates basic JSON encoding and decoding operations. Here’s an explanation of the code:

  1. The program defines a file to store JSON data and a working storage section to hold the data structure.

  2. In the ENCODE-JSON procedure:

    • We set values for the page number and fruits.
    • We construct a JSON string using the STRING verb, which is then written to a file.
  3. In the DECODE-JSON procedure:

    • We read the JSON data from the file.
    • We use the UNSTRING verb to parse the JSON string and extract values.
    • The extracted values are then displayed.

Note that COBOL doesn’t have built-in JSON support like Go does. This example provides a basic simulation of JSON encoding and decoding. For more complex JSON operations in COBOL, you would typically use third-party libraries or more sophisticated parsing techniques.

To run this program:

  1. Save the code in a file with a .cob extension (e.g., json-example.cob).
  2. Compile the program using a COBOL compiler.
  3. Execute the compiled program.

The program will create a file named json-data.txt with the encoded JSON data, then read and decode this data, displaying the results.

This example demonstrates basic JSON-like operations in COBOL, but it’s important to note that it’s a simplified representation and doesn’t cover all aspects of JSON handling that the Go example does.