Skip to main content

Use DATA-INTO for Processing Data With RPG

XML-INTO helped me with processing XML data, but now I’m being asked to process JSON, CSVs and other types of data. Can RPG help me to do this?

Q: XML-INTO helped me with processing XML data, but now I’m being asked to process JSON, CSVs and other types of data. Can RPG help me to do this?

Everyone knows about RPG’s flexibility of interface when it comes to accessing Db2* for i data. But what about other kinds of data? The following data formats should look familiar:

•	12345,”Mountain Co-op”,”Ashton Bridge”,CA

 
•	[ { “customer”: 12345,”name”: “Mountain Co-op”,”city”: “Ashton 

Bridge”,”state” : “CA” } ]


•	customer=12345  name=Mountain Co-op City=Ashton Bridge state=CA

In case they don’t all ring a bell for you: The first is CSV (character-separated values) text; the second is JSON (JavaScript object notation); and the third is often referred to as properties or name/value text, and in slightly different form in a URL it is known as the query string.

They’re all commonly used ways of defining data for interchange between systems. Add XML to the list and you’ve covered a very large percentage of all the current formats used for data interchange. But for RPG users, they all share another characteristic: They’re examples of the kind of data that RPG’s new DATA-INTO operation is designed to process.

DATA-INTO provides a facility very similar to XML-INTO but rather than processing only XML, it can process any data for which a parser has been written. And anyone can write a parser!

DATA-INTO is IBM’s response to repeated requests for an XML-INTO-like facility for JSON. Wisely, IBM decided not to limit its capability to JSON, but rather to make it a generic user-extensible feature that can be expanded and enhanced over the years to accommodate whatever data interchange method may be tomorrow’s “flavor of the month.”

It works in a similar manner to XML-INTO (i.e., it takes a stream of data and splits it into its component fields). The big difference being that for XML-INTO, the data format is dictated by the XML specifications. For DATA-INTO, the data format is whatever the parser has been designed to handle.

Speaking of parsers, what’s available today? IBM supplies two example parsers with the compiler. The first (there are actually two variants) processes properties (name/value) text. The second is a sample JSON parser. While the latter works for most situations, it’s not intended to be production code. For production purposes, I suggest using YAJLINTO, a JSON parser developed by Scott Klement based upon his port of the widely used YAJL JSON parser. You can get more information on this parser and download the latest version here: scottklement.com/yajl/.

The other option (that I’m aware of) is a CSV parser that Susan Gantner and I developed and described in “A Closer Look at RPG’s DATA-INTO”. However, if you want to fully understand how DATA-INTO parsers work with a view to writing your own, or to modifying ours, then you should also read the preceding article where we introduced the basics of DATA-INTO (bit.ly/2UVymF4).

DATA-INTO is a great example of a new dynamic in the RPG compiler world. This makes perfect sense because the world of IT is moving at an ever-increasing pace and it’s vital that RPG is ready to play in whatever new arena your applications enter.

Jon Paris is a technical editor for IBM Systems magazine, Power Systems. He’s also the cofounder of the RPG & DB2 Summit and a partner with Partner400.