JSON PARSE and what to do instead
Writing JSON works in this compiler, as the previous lesson showed. Reading it does not: JSON PARSE is one of the statements GnuCOBOL 3.2 accepts and does not implement. This page shows the run that proves it and then the parser a real program uses for the four fields it needs.
JSON PARSE builds and does nothing
The statement looks like its sibling of the previous lesson:
IDENTIFICATION DIVISION.
PROGRAM-ID. P1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CUS-REC.
05 CUS-ID PIC 9(3).
05 CUS-NAME PIC X(10).
05 CUS-BAL PIC 9(5).
01 JSON-IN PIC X(200) VALUE
'{"CUS-REC":{"CUS-ID":42,"CUS-NAME":"Ada","CUS-BAL":1250}}'.
PROCEDURE DIVISION.
JSON PARSE JSON-IN INTO CUS-REC
END-JSON.
DISPLAY "id " CUS-ID.
DISPLAY "name [" CUS-NAME "]".
DISPLAY "bal " CUS-BAL.
STOP RUN.
cobc -x -free -Wall -o p1 p1.cob
./p1
p1.cob:12: warning: JSON PARSE is not implemented [-Wpending]
10 | '{"CUS-REC":{"CUS-ID":42,"CUS-NAME":"Ada","CUS-BAL":1250}}'.
11 | PROCEDURE DIVISION.
12 > JSON PARSE JSON-IN INTO CUS-REC
13 | END-JSON.
14 | DISPLAY "id " CUS-ID.
p1.cob:13: warning: JSON PARSE is not implemented [-Wpending]
11 | PROCEDURE DIVISION.
12 | JSON PARSE JSON-IN INTO CUS-REC
13 > END-JSON.
14 | DISPLAY "id " CUS-ID.
15 | DISPLAY "name [" CUS-NAME "]".
id 000
name [ ]
bal 00000
Two warnings, one per part of the statement, and then the fields exactly as they were declared: no value, no name and no balance. The build succeeds and the run exits with 0, so a job that trusts this statement reads zeros and reports success. That is the worst shape of a missing feature, and the reason the lesson has a second half.
A parser with INSPECT
The line a batch job reads is usually flat and small, and four fields of it are the ones the program needs. INSPECT ... TALLYING FOR CHARACTERS BEFORE finds a key and answers the number of characters in front of it; reference modification cuts the value that follows:
IDENTIFICATION DIVISION.
PROGRAM-ID. PARSE1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 JSON-LINE PIC X(200) VALUE
'{"ID":"A100","AMOUNT":1250,"STATUS":"OK"}'.
01 WS-KEY PIC X(20).
01 WS-KLEN PIC 9(2).
01 WS-VALUE PIC X(40).
01 WS-POS PIC 9(3).
01 WS-TAIL PIC X(200).
01 WS-LEN PIC 9(3).
01 WS-FOUND PIC X VALUE "N".
PROCEDURE DIVISION.
COMPUTE WS-LEN = FUNCTION STORED-CHAR-LENGTH(JSON-LINE)
MOVE '"ID":' TO WS-KEY
MOVE 5 TO WS-KLEN
PERFORM FIND-VALUE
DISPLAY "id [" FUNCTION TRIM(WS-VALUE) "] found " WS-FOUND
MOVE '"AMOUNT":' TO WS-KEY
MOVE 9 TO WS-KLEN
PERFORM FIND-VALUE
DISPLAY "amount [" FUNCTION TRIM(WS-VALUE) "] found " WS-FOUND
MOVE '"STATUS":' TO WS-KEY
MOVE 9 TO WS-KLEN
PERFORM FIND-VALUE
DISPLAY "status [" FUNCTION TRIM(WS-VALUE) "] found " WS-FOUND
MOVE '"OWNER":' TO WS-KEY
MOVE 8 TO WS-KLEN
PERFORM FIND-VALUE
DISPLAY "owner [" FUNCTION TRIM(WS-VALUE) "] found " WS-FOUND.
STOP RUN.
FIND-VALUE.
MOVE SPACES TO WS-VALUE
MOVE "N" TO WS-FOUND
MOVE 0 TO WS-POS
INSPECT JSON-LINE TALLYING WS-POS
FOR CHARACTERS BEFORE WS-KEY(1:WS-KLEN)
IF WS-POS IS GREATER THAN OR EQUAL TO WS-LEN
EXIT PARAGRAPH
END-IF
MOVE "Y" TO WS-FOUND
COMPUTE WS-POS = WS-POS + WS-KLEN + 1
MOVE JSON-LINE(WS-POS:WS-LEN) TO WS-TAIL
UNSTRING WS-TAIL DELIMITED BY "," OR "}"
INTO WS-VALUE
END-UNSTRING
INSPECT WS-VALUE REPLACING ALL '"' BY SPACE
MOVE FUNCTION TRIM(WS-VALUE) TO WS-VALUE.
cobc -x -free -Wall -o parse1 parse1.cob
./parse1
id [A100] found Y
amount [1250] found Y
status [OK] found Y
owner [] found N
The four fields came out, the missing one answered N and left its value empty. Three details make the paragraph work and are worth naming.
The key is compared through a reference modification, WS-KEY(1:WS-KLEN), because INSPECT compares the whole field and would look for followed by fifteen spaces. INSPECT has no "not found" answer: when the key is absent it counts every character of the line, so the paragraph compares the count with the length of the line. And the value is cut from a copy of the rest of the line, delimited by the comma or the closing brace that follow it, which is all a flat object needs.
A key that is not there
The paragraph answered N for and left the value blank, which is the behaviour the caller has to decide about. A required key that is missing is not a parse error in this design; it is a field that stayed empty, and the program that reads it must say so:
IF WS-FOUND = "N" after a required key is the line that turns a silent zero into a rejected record with a message, which is the file-error machinery of a previous lesson. The same paragraph handles numbers and strings; the number 1250 of the amount arrived as four characters and a later MOVE or FUNCTION NUMVAL turns it into a number.
The next lesson takes the same look at XML, where the compiler implements a little more than it does for JSON and still not everything the standard describes.
Exercises
- Parse a second line with a nested object and say which of its keys the paragraph finds.
- Add the key to the line and print it as a number with FUNCTION NUMVAL.
- Make the paragraph answer a level-88 condition instead of WS-FOUND.
- Read a line whose value contains a comma inside quotes and explain what the UNSTRING does to it.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does JSON PARSE do in GnuCOBOL 3.2?
Answer
Nothing: it builds with a -Wpending warning and leaves the record empty
The run printed 000, spaces and 00000 for a document that held 42, Ada and 1250.
-
Which statement finds the position of a key in a line?
Answer
INSPECT ... TALLYING FOR CHARACTERS BEFORE the key
The parser counted the characters in front of each key with INSPECT.
-
Why is the key compared through WS-KEY(1:WS-KLEN)?
Answer
INSPECT compares the whole field, and the padding spaces would be part of the key
Without the reference modification nothing was found and every key answered N.
-
How does the paragraph know that a key was not found?
Answer
INSPECT counts every character of the line in that case, so the count equals the length
The paragraph compares WS-POS with the stored length of the line.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-LINE PIC X(40) VALUE "ID=42;NAME=ADA". 01 W-POS PIC 9(2) VALUE 0. PROCEDURE DIVISION. INSPECT W-LINE TALLYING W-POS FOR CHARACTERS BEFORE "NAME" DISPLAY W-POS. STOP RUN.Answer
06
ID=42; is six characters before the N of NAME.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-TAIL PIC X(20) VALUE "1250,STATUS:OK". 01 W-VAL PIC X(10). PROCEDURE DIVISION. UNSTRING W-TAIL DELIMITED BY "," INTO W-VAL END-UNSTRING. DISPLAY "[" FUNCTION TRIM(W-VAL) "]". STOP RUN.Answer
[1250]
The value stops at the first comma.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-V PIC X(12) VALUE "ADA ". 01 W-O PIC X(6). PROCEDURE DIVISION. INSPECT W-V REPLACING ALL "A" BY "X". MOVE W-V TO W-O. DISPLAY "[" FUNCTION TRIM(W-O) "]". STOP RUN.Answer
[XDX]
Both letters A of the field were replaced, and the trim removed the padding.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-F PIC X VALUE "N". 01 W-K PIC X(4) VALUE "OWNR". PROCEDURE DIVISION. IF W-K IS NOT EQUAL TO "OWNER" MOVE "Y" TO W-F END-IF. DISPLAY W-F. STOP RUN.Answer
Y
The four-character key is not the six-character one, so the flag changes.
-
What should a program do when a required key answers N?
Answer
Reject the record with a message and a status, as a file error is rejected
The value stays empty, so silence turns into a zero in the report.
-
Which part of the JSON of these lessons does the hand-written parser not handle?
Answer
A nested object, because it reads from the start of the line and stops at the first delimiter
The parser reads a flat object, as its own line and paragraph show.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev