, 3 min read
Generating JSON with COBOL
Original post is here eklausmeier.goip.de/blog/2021/08-17-generating-json-with-cobol.
1. Problem statement. Generate JSON from COBOL WORKING-STORAGE
section memory area. GnuCOBOL can generate and parse JSON strings. Example.
IDENTIFICATION DIVISION.
PROGRAM-ID. json.
AUTHOR. Elmar Klausmeier.
DATE-WRITTEN. 12-Aug-2021.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 j usage binary-long value 3.
77 jsono pic x(256) value spaces.
01 a1.
02 b.
03 c.
04 d pic x value 'd'.
02 e pic x value 'e'.
01 rec.
03 a PIC X(3) VALUE 'A'.
03 b PIC X(3) VALUE ALL 'B'.
03 c.
05 d PIC X(3) VALUE SPACES.
PROCEDURE DIVISION.
DISPLAY "Converting from COBOL to JSON".
DISPLAY "a1 = " a1.
JSON GENERATE jsono FROM a1 ON EXCEPTION
display 'cannot generate json from a1'.
DISPLAY "jsono = " function trim(jsono).
DISPLAY "rec = " rec.
JSON GENERATE jsono FROM rec ON EXCEPTION
display 'cannot generate json from rec'.
DISPLAY "jsono = " function trim(jsono).
STOP RUN.
This will print:
a1 = de
jsono = {"a1":{"b":{"c":{"d":"d"}},"e":"e"}}
rec = A BBB
jsono = {"rec":{"a":"A","b":"BBB","c":{"d":" "}}}
GnuCOBOL does not support occurs
-items in JSON GENERATE
. For example, below code will not compile:
01 a1.
02 b.
03 c occurs 0 to 2 depending j.
04 d pic x value 'd'.
02 e pic x value 'e'.
Error message:
$ cobc -x json.cbl
json.cbl:17: error: 'c' cannot have OCCURS DEPENDING because of 'e'
json.cbl:39: warning: OCCURS items in JSON/XML GENERATE is not implemented [-Wpending]
Initially, Arch Linux GnuCOBOL was not able to properly handle JSON GENERATE
. That there was something wrong with JSON GENERATE
in GnuCOBOL was hinted in the NEWS file. The NEWS
file of GnuCOBOL contains the following relevant information:
** JSON GENERATE statement
(note: runtime support needs additional library cJSON or JSON-C)
GnuCOBOL also does not support JSON PARSE
.
2. Installation. The Arch Linux gnucobol package unfortunately has a bug regarding JSON GENERATE
. Therefore you have to build the compiler yourself, or install open-cobol instead.
Configure.
$ time ./configure
...
configure: COB_MODULE_EXT so
configure: COB_EXE_EXT
configure: COB_SHARED_OPT -shared
configure: COB_PIC_FLAGS -fPIC -DPIC
configure: COB_EXPORT_DYN -Wl,--export-dynamic
configure: COB_STRIP_CMD strip --strip-unneeded
configure: Dynamic loading: System
configure: Use gettext for international messages: yes
configure: Use fcntl for file locking: yes
configure: Use math multiple precision library: gmp
configure: Use curses library for screen I/O: ncurses
configure: Use Berkeley DB for INDEXED I/O: yes
configure: Used for XML I/O: libxml2
configure: Used for JSON I/O: json-c
real 11.63s
user 8.25s
sys 0
swapped 0
total space 0
Now compile the compiler.
$ time make
real 46.61s
user 45.22s
sys 0
swapped 0
total space 0
Switch to root user, then install.
# make install
I uninstalled the previously installed COBOL compiler via pacman -R gnucobol
. Unfortunately, the Arch Linux package does not react to bug
I adopted previously orphaned package open-cobol and corrected above bug.
Added 18-Apr-2024: IBM COBOL for Linux currently does not support neither JSON GENERATE
nor JSON PARSE
.
From Language elements:
JSON GENERATE and JSON PARSE statements: JSON is not currently supported in COBOL for Linux on x86.
I.e., GnuCOBOL is, so to say, better than IBM's own COBOL. Apparently IBM isn't putting too much effort into COBOL, see Memory Limitations with IBM Enterprise COBOL Compiler.