jsoncobolmainframecicssuppress

Suppressing array data element in COBOL JSON GENERATE


I am trying to generate a JSON from the below Cobol structure using COBOL GENERATE statement of COBOL 6.3. I would like suppress the data elements in array based on their values. For example, would like to suppress model-value (ws-inx1,ws-inx2) in case if the value is spaces.

Can someone help me how I can achieve this using JSON GENERATE statement?

If not possible with JSON GENERATE , is there anyway we can generate JSON in a COBOL-CICS program. I have checked the possibility of CICS JSON assist, but could not see any suppress option.

COBOL copybook structure.

01 payload
   03 reference-id             pic x(10).
   03 model-specific-data     occurs 5 times indexed by ws-inx1.
       05 model-id            pic x(10).
       05 model-values        occurs 5 times indexed by ws-inx2.
           10 model-attrib-name     pic x(01).
           10 model-value           pic x(3).
           10 model-value-boolean   pic x(05).
               88 true-val          value 'true'.
               88 false-val         value 'false'

Sample JSON expected

{
    "payload": {
        "reference-id": "abc123",
        "model-specific-data": [
            {
                "model-id": "model 01",
                "model-value-attribs": [
                    {
                        "model-attrib-name": "a",
                        "model-value": "A01",
                        "model-value-boolean": true
                    },
                    {
                        "model-attrib-name": "b",
                        "model-value": "123",
                        "model-value-boolean": false
                    },
                    {
                        "model-attrib-name": "x",
                        "model-value-boolean": false
                    }
                ]
            },
            {
                "model-id": "model 02",
                "model-value-attribs": [
                    {
                        "model-attrib-name": "c",
                        "model-value": "C01",
                        "model-value-boolean": true
                    },
                    {
                        "model-attrib-name": "d",
                        "model-value": "D01",
                        "model-value-boolean": false
                    }
                ]
            }
        ]
    }
}

Solution

  • Conditional suppress doesn´t seem to be possible you might consider create with own logic. JSON GENERATE creates utf-8 encoding if you define json fields usage UTF-8 you can do the same. Ex. 01 UTF8-Data Pic U(2) Usage UTF-8. . . . Move "AB" to UTF8-Data

    Sorry I was a little too quick with my previous reply. JSON GENERATE does have everything to obtain what you want. I only changed boolean to 1 character for JSON GENERATE expects 1 char only for boolean.

    Source:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. JSONGEN.
       AUTHOR. FRANS BEYL.
       ENVIRONMENT DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 payload.
           03 reference-id                     pic x(10).
           03 model-specific-data   occurs 5 times
                                    indexed by ws-inx1.
                   05 model-id                 pic x(10).
                   05 model-values  occurs 5 times
                                    indexed by ws-inx2.
                       10 model-attrib-name     pic x(01).
                       10 model-value           pic x(3).
                       10 model-value-boolean   pic x.
                           88 true-val          value 't'.
    
    
       01  PAYLOAD-JSON                         pic U(500).
       01  JSON-LEN                             pic S9(4) comp.
    
       PROCEDURE DIVISION.
      *
           Perform 1000-init-data.
           Perform 2000-create-json.
           Perform 9000-ending.
           GOBACK.
    
       1000-init-data.
      *
           move spaces to payload payload-json.
           move "abc123"   to REFERENCE-ID.
    
           set ws-inx1 to 1.
           move "model 01" to MODEL-ID(ws-inx1).
    
           set ws-inx2 to 1.
           move "a"        to model-attrib-name(ws-inx1, ws-inx2).
           move "A01"      to model-value(ws-inx1, ws-inx2).
           move "t"     to model-value-boolean(ws-inx1, ws-inx2).
    
           set ws-inx2 to 2.
           move "b"        to model-attrib-name(ws-inx1, ws-inx2).
           move "123"      to model-value(ws-inx1, ws-inx2).
           move "f"     to model-value-boolean(ws-inx1, ws-inx2).
    
           set ws-inx2 to 3.
           move "x"        to model-attrib-name(ws-inx1, ws-inx2).
           move "f"     to model-value-boolean(ws-inx1, ws-inx2).
    
           set ws-inx1 to 2.
           move "model 02" to MODEL-ID(ws-inx1).
    
           set ws-inx2 to 1.
           move "c"        to model-attrib-name(ws-inx1, ws-inx2).
           move "C01"      to model-value(ws-inx1, ws-inx2).
           move "t"     to model-value-boolean(ws-inx1, ws-inx2).
    
           set ws-inx2 to 2.
           move "d"        to model-attrib-name(ws-inx1, ws-inx2).
           move "D01"      to model-value(ws-inx1, ws-inx2).
           move "f"     to model-value-boolean(ws-inx1, ws-inx2).
    
       2000-create-json.
      *
           JSON GENERATE PAYLOAD-JSON FROM PAYLOAD
               COUNT json-len
               SUPPRESS EVERY NONNUMERIC when SPACES
               CONVERTING model-value-boolean TO BOOLEAN USING true-val
           end-json.
    
       9000-ending.
      *
           DISPLAY  json-len.
           display function display-of(
                      function national-of(PAYLOAD-JSON 1208)
                   1047).
    

    Result displayed: 0491 {"payload":{"reference-id":"abc123","model-specific-data":[{"model-id":"model 01","model-values":[{"model-attrib-name":" a","model-value":"A01","model-value-boolean":true},{"model-attrib-name":"b","model-value":"123","model-value-boolean":fa lse},{"model-attrib-name":"x","model-value-boolean":false}]},{"model-id":"model 02","model-values":[{"model-attrib-name" :"c","model-value":"C01","model-value-boolean":true},{"model-attrib-name":"d","model-value":"D01","model-value-boolean": false}]}]}}