JSON has pretty much taken over from XML as the main markup language used for exchanging data between services and interfaces. XML is good but can also be quite complex, and JSON has come forward as a much lighter and leaner way of describing your data.
So of course at some point we will face the need to exchange data in the IBMi RPG world. Might be that we need to exchange data with external services that already talk JSON. Or possibly that we want to build a new internal interface and we want to use JSON and take advantage of its efficiencies in describing our data.
There are a couple of options for processing JSON within RPG, and of course im excluding the option of hand coding some sort of JSON processor based on doing some complicated string handling, as that would likely be a lot of work and quite brittle. The best two options that we have are either
- Use DB2 JSON handling operations
- Use the DATA-GEN and DATA-INTO RPG opcodes and combine them with the latest version of Scott Klements IBMi implementation of the YAJL library
The DB2 JSON operation do work, but they is an element of learning curve there, and on the side of generating JSON, the DB2 options is fairly cumbersome, and could lead to some brittle code also.The more natural approach we will look at here is using DATA-GEN and DATA-INTO in our RPG programs. Combined with the parsers provided with the YAJL library, makes the reading and writing of JSON a relatively straightforward task within RPG.