Generating JSON anonymous arrays
You can generate JSON anonymous arrays by specifying the data name of the table on the JSON GENERATE statement while using the NAME IS OMITTED phrase.
Consider the following example:
Identification division.
Program-id. myprog.
Data division.
Working-storage section.
1 ACT.
2 B1 occurs 2.
3 C1.
4 M1 pic 9.
4 D1 occurs 2.
5 N1 pic 9.
1 json-text pic u dynamic.
Procedure division.
move 1 to M1(1)
move 2 to M1(2)
move 3 to N1(1 1)
move 4 to N1(1 2)
move 5 to N1(2 1)
move 6 to N1(2 2)
move spaces to json-text
json generate json-text from b1
name b1 is omitted
end-json
goback
.
End program myprog.
Running the program will produce the following JSON text in
the item
json-text
:[{"C1":{"M1":1,"D1":[{"N1":3},{"N1":4}]}},{"C1":{"M1":2,"D1":[{"N1":5},{"N1":6}]}}]
Note
that the JSON GENERATE sender b1
was not subscripted in order to refer to all
occurrences of b1
.Consider the next example where the table
b1
has two dimensions and is nested
within another table like below: Identification division.
Program-id. myprog.
Data division.
Working-storage section.
1 ACT.
2 TOPTABLE occurs 3.
3 B1 occurs 2.
4 C1.
5 M1 pic 9.
5 D1 occurs 2.
6 N1 pic 9.
1 json-text pic u dynamic.
Procedure division.
move 1 to M1(1 1)
move 2 to M1(1 2)
move 3 to N1(1 1 1)
move 4 to N1(1 1 2)
move 5 to N1(1 2 1)
move 6 to N1(1 2 2)
move spaces to json-text
json generate json-text from b1(1)
name b1 is omitted
end-json
goback
.
End program myprog.
Running the program will produce the following JSON text in the item
json-text
:[{"C1":{"M1":1,"D1":[{"N1":3},{"N1":4}]}},{"C1":{"M1":2,"D1":[{"N1":5},{"N1":6}]}}]
Note
that b1(1)
was specified with a single index subscript on the JSON GENERATE
statement. The value of the subscript 1
indicates that the first occurrence
of table TOPTABLE shall be generated. The omission of a second index indicates to the JSON
GENERATE statement that the statement shall operate on the entire table b1
(rather than operating on a single occurrence of b1
, which is what would have
occurred if a second index subscript was specified). This pattern applies generally to
multiple nested tables, that is, an anonymous array can be generated by the combination of the
NAME IS OMITTED phrase and specifying the sending item with one less subscript than its
dimension.