How to build complex JSON response?

It is possible to create any complex response using aperïo, but there are couple of limitations:

  • The response is prepared in job memory which is limited to 16MB per job. These 16 MB are total available memory limited by IBM in single cheap storage model. This is more than enough if someone wants to create web services in micro service style. Iptor approach is performance and micro services structure of API. That is why we stick to straight requests/responses. Every time when RPG program is created the programmer must to take it into consideration, that job memory is limited. The other factor is reasonable response time. It may happen that creating verry complex and big response takes long time.

  • Aperio has predefined structure request and response. It means the response data is ALWAYS put into “data” object in JSON…

old protocol:

    {
        "IptorAPI":"1.0",
        "data":{">>here is place for Your response<<"},
        "id":"js069cty"
    }

New protocol

    {
        "data":{">>here is place for Your response<<"}
    }
  • The complex response can’t be described by metadata or it will be very hard to describe it. Aperïo metadata is designed to describe simple elements which are later on rendered to HTML code. The complex response will not be rendered at all, and can’t be reused to create mobile application. So, in case complex response the metadata will only contain the name of RPG program and name of method.

There are at least three ways to create complex response:

  • Using response builder
  • Using JSON builder, then put produced JSON into response
  • Use any other JSON builder and create string variable which will contain JSON. Then and put it into response.

In QSAMPLES source file there are two examples how to build complex response:

  • EXAMPLE012 - This program builds complex response using standard aperïo procedures (respAddelement(), respAddField(), respAdd…Value()). The structure of response can be then described by metadata
  • EXAMPLE013 - This program builds complex response using JSON object. First the complex JSON structure is created. Then the entire JSON is added to response. NOTE: In current aperïo JSON builder there is no possibility to add JSON object dirrectly to ‘data’ element of response.

program EXAMPLE012 creates following response:

{
    "IptorAPI":"1.0",
    "data":{
        "itemCodes":[
            "11991-ABC",
            "11992-CDE",
            "11993-FGH"
        ],
        "alternates":[
            "11991",
            "11992",
            "11993"
        ],
        "itemTypes":[
            "A",
            "B",
            "C"
        ],
        "dates":[
            "2019-02-22",
            "2019-02-23",
            "2019-02-24"
        ],
        "flags":[
            true,
            false
        ],
        "onHands":[
            0,
            0,
            0
        ],
        "details":[
            {
                "det01":"ABC",
                "det02":123
            },
            {
                "det01":"DEF",
                "det02":456
            },
            {
                "det01":"GHI",
                "det02":789
            }
        ],
        "details2":[
            {
                "det201":"YYYY",
                "det202":1234,
                "det203":99,
                "sub-details2":[
                    {
                        "det2201":"aaaa",
                        "det2202":1234
                    },
                    {
                        "det2201":"bbbb",
                        "det2202":5678
                    }
                ]
            },
            {
                "det201":"ZZZZ",
                "det202":5678,
                "det203":88,
                "sub-details2":[
                    {
                        "det2201":"cccc",
                        "det2202":7777
                    },
                    {
                        "det2201":"dddd",
                        "det2202":8888
                    }
                ]
            }
        ]
    },
    "id":"jxedanwt"
}

program EXAMPLE013 creates following response:

{
    "IptorAPI":"1.0",
    "data":{
        "myJSON":{
            "field1":"ABC",
            "field2":"DEF",
            "field3":123,
            "field4":456,
            "sub-object":{
                "field3":"ABC",
                "field4":"DEF",
                "booleanField1":false,
                "booleanField2":true,
                "nullField":null
            },
            "sub-array":[
                "Value1",
                "Value2",
                "Value3"
            ]
        }
    },
    "id":"jxedkugz"
}