Guru: Parsing JSON With DATA-INTO And A Twist
September 23, 2019 Mike Larsen
One of my recent projects required me to parse JSON returned from a web service. On the surface this sounded like a pretty easy task, but I quickly ran into a challenge. The JSON being retuned didn’t have a top-level element, and since I wanted to load the JSON into a data structure, my program couldn’t handle it.
After some searching, I found that some of my options included changing the code in the parser (JSONPARSE) or using a totally different parser altogether. While both of these are viable options, I decided to take a slightly different route.
Before I explain the solution, it makes sense to show what the JSON looks like (Figure 1).
This story contains code, which you can download here.
When using DATA-INTO with a data structure, all elements of the data structure must match exactly to the JSON being loaded into it. When I create a data structure, I need to name it and each variable within. But, as you can see, there isn’t a top-level element in my JSON. Therein lies the problem; I have to name the top-level of my data structure and it needs to match the top-level of the JSON. So, when I created a data structure that I wanted to hold the JSON (Figure 2), DATA-INTO fails because the variables in the data structure don’t exactly match the JSON object (it fails at the top-level ‘itemDs’).
The following code shows the data structure to contain JSON.
dcl-ds itemDs Qualified; success char(5); classdesc char(30); numberofitems char(5); dcl-ds itemlist dim(50) inz; itemnumber char(5); description char(30); unitofmeasure char(5); itemclass char(5); end-ds itemlist; end-ds itemDs;
So, without modifying the parser or using a different parser, how did I overcome this challenge? The answer is to add a top-level element to the JSON being returned from the web service. Ok, that sound great, but how do I do that?
I’m using the SQL function HTTPGETCLOB to consume the web service, so all I have to do is wrap the JSON result within a JSON object. The JSON object will now be the highest level and I named it to match the top-level in my data structure (itemDs). This next piece of code shows HTTPGETCLOB with concatenated JSON object.
// since the highest level of the json array isn't named, we need to // apply one to it so we can have it match the data structure we're // loading the json into. That is done by concatenating ' "itemDs" : ' // with the HttpGetClob function. Exec Sql Select '{ "itemDs" : ' concat Systools.HttpGetClob (:WebServiceUrl , :WebServiceHeaderJson) concat '}' into :itemJson from sysibm.sysdummy1;
With the JSON object in place, I can now parse it using DATA-INTO and the data structure I built.
Data-into itemDs %Data(%trim(itemJson): 'case=any allowextra=yes') %Parser('JSONTSTLIB/JSONPARSE');
Note that I’ve also used this technique in the past using the SQL function JSON_TABLE and it works the same.
There are often many different solutions to problems. I hope this serves as another option if you’re faced with a similar situation. The complete code for consuming a web service and parsing the returned JSON is available for download.
While the proposed solution will work well in this set of circumstances it still has what for me remains a major problem. Namely that the JSON parser supplied by IBM was never intended for production work – it was intended to demonstrate how to write a parser. The issue encountered by the writer is a direct result of this not being “production ready” – Scott Klement’s YAJLINTO parser for example does not suffer this problem.
Do it in Python
If the web service (or whatever) consuming the JSON is written in RPG why one earth would you want to jump through hoops trying to interface with Python for this? Python makes a lot of sense for many, many tasks but I think lately it has started to be proposed as the solution to everything. This has been happening with SQL for some time – I guess it is JSON’s turn.
“The answer is Python! – Now what was your question?”