Alain Reymond - 2024-06-05

Hello,

Gnucobol does not handle occurs item in JSON GENERATE.
As occurs is not implemented, a code like this :

       1 person.
         3 firstName pic x(50).
         3 lastName  pic x(50).
         3 children.
           5 child pic x(50) occurs 10.
         3 pets pic x(10) occurs 5 .

       1 num-of-children pic 9(4) comp-5 value 0.
       1 num-of-pets     pic 9(4) comp-5 value 0.

       1 json-doc pic x(400).
       1 c1 pic 9(4) comp-5.
       1 c1-display pic 999999999.

       procedure division.
           move 'Mark'  to firstName
           move 'Jones' to lastName

           move 3 to num-of-pets
           move 'cat' to pets(1)
           move 'dog' to pets(2)
           move 'canary' to pets(3)

           move 2 to num-of-children
           move "Paul" to child(1)
           move "Robert" to child(2)

           json generate json-doc from person 
             count in c1
           end-json

will display that:

{"person":{"firstName":"Mark","lastName":"Jones","children":{"child":"Paul"},"pets":"cat"}}

Robert is omitted.
And something like:

       1 person.
         3 firstName pic x(50).
         3 lastName  pic x(50).
         3 children.
           *>5 child pic x(50) occurs 10.
           4 child1 pic x(50).
           4 child2 pic x(50).
           4 child3 pic x(50).
           4 child4 pic x(50).
           4 child5 pic x(50).
         3 pets pic x(10) occurs 5 .
...
           move "Paul" to child1
           move "Robert" to child2
...
           json generate json-doc from person 
             count in c1
           end-json

will create:

{"person":{"firstName":"Mark","lastName":"Jones","children":{"child1":"Paul","child2":"Robert","child3":" ","child4":" ","child5":" "},"pets":"cat"}}

which is obviously not what we expect with child1, child2, etc....

Is there a workaround to create json strings with the equivalent of occurs ?

Regards.