Combining JSON
Posted: Fri Jun 10, 2022 3:00 pm
I've used SK's YAJL for quite a few years - absolutely cool tool. But as I got more familiar with it our company has expanded what we can do with it. Anyway we have quite a few Web Services running using this tool but I started to think about trying to reuse the JSON between RPG programs as opposed to just a response to APACHE. What I'm interested in doing is calling a program from a main program, the sub program returns the JSON (currently in a buffer) and add the JSON to he JSON document in the main program. Seems a relatively easy thing to do - I used yajl_buf_load_tree to create a Node and then yajl_genFromNode to add it to the main programs JSON document.
What I noticed was that I had to make sure that the code that did the yajl_buf_load_tree was carried out before the yajl_genOpen or else it would fail. I think the issue is that once yajl_genOpen has been run the the generator handle is gGenerator whereas if yajl_genOpen hasn't be run the yajl_buf_load_tree uses an arbitrary handler to create the node, then the node can be added to the document.
So this works - getAddress is just a subprogram that returns an address and flood score as JSON in the buffer
But if the getAddress is called after yajl_genOpen it fails, so this code fails
In reality I can code it like version 1 most of the time. But the use case that I'm trying to solve is getting a list of customers. So in this instance the main programs JSON would build an array of customers by call the sub program for each customer. The sub progam is already built and it accepts a customer ID and returns a full customer JSON. But I only want the address portion of the JSON, I don't want anything else from the address - ignore flood scores etc. So that kind of rules out using yajl_addPreformattedPtr because I need to traverse the JSON to find the correct subnode of just the address.
If it could be made to work in some fashion the advantage is that if any changes were made in the sub program then it bubbles up to the main program without major rework or recompiles. So for example in Ireland we have eventually added our version of the ZIP code called the Eircode. So in this instance we would only have to add the Eircode key :value pair into the Address node in the subprogram, it should then appear in the main program's JSON.
I'm probably not explaining myself too well - the main idea of mine is to try to modularise the JSON programs but be able to pass the JSON data between programs so they can be combined into other JSON documents.
Anyway if this is impossible I don't mind at all. And if Scott reads this - again thank you so much for a brilliant tool.
Eddie
What I noticed was that I had to make sure that the code that did the yajl_buf_load_tree was carried out before the yajl_genOpen or else it would fail. I think the issue is that once yajl_genOpen has been run the the generator handle is gGenerator whereas if yajl_genOpen hasn't be run the yajl_buf_load_tree uses an arbitrary handler to create the node, then the node can be added to the document.
So this works - getAddress is just a subprogram that returns an address and flood score as JSON in the buffer
Code: Select all
getAddress(p_buffer :p_bufferlen);
addressNode = yajl_buf_load_tree(p_buffer :p_bufferlen :errMsg);
yajl_genOpen(jsonPretty());
yajl_beginObj();
yajl_addChar('type': 'person');
yajl_addChar('name': 'Eddie');
rc = yajl_genFromNode('address'
:YAJL_object_find(addressNode: 'address'));
yajl_tree_free(addressNode);
yajl_endObj();
if (not isError);
httpStatus = 200;
else;
httpStatus = 400;
endif;
yajl_writeStdout(httpStatus:errorDescription);
Code: Select all
yajl_genOpen(jsonPretty());
yajl_beginObj();
yajl_addChar('type': 'person');
yajl_addChar('name': 'Eddie');
getAddress(p_buffer :p_bufferlen);
addressNode = yajl_buf_load_tree(p_buffer :p_bufferlen :errMsg);
rc = yajl_genFromNode('address'
:YAJL_object_find(addressNode: 'address'));
yajl_tree_free(addressNode);
yajl_endObj();
if (not isError);
httpStatus = 200;
else;
httpStatus = 400;
endif;
If it could be made to work in some fashion the advantage is that if any changes were made in the sub program then it bubbles up to the main program without major rework or recompiles. So for example in Ireland we have eventually added our version of the ZIP code called the Eircode. So in this instance we would only have to add the Eircode key :value pair into the Address node in the subprogram, it should then appear in the main program's JSON.
I'm probably not explaining myself too well - the main idea of mine is to try to modularise the JSON programs but be able to pass the JSON data between programs so they can be combined into other JSON documents.
Anyway if this is impossible I don't mind at all. And if Scott reads this - again thank you so much for a brilliant tool.
Eddie