Read Payload from IFS File to a string in RPGLE

Discussions relating to the ScottKlement.com port of the open source YAJL JSON Reader/Generator. This includes the YAJL tool as well as the YAJLR4, YAJLGEN, YAJLINTO and YAJLDTAGEN add-ons from ScottKlement.com. http://www.scottklement.com/yajl/
Post Reply
cooldude
Posts: 15
Joined: Wed Feb 12, 2025 5:07 am

Read Payload from IFS File to a string in RPGLE

Post by cooldude »

Hi,
Would like to check on this below scenario, please assist. Thanks!
Trying to read a JSON payload from an IFS file to a string. But ContentLength comes as 0. Any thoughts on what am I missing?

Dcl-S docNode like(yajl_val);
Dcl-S errmsg varchar(500) inz('');
Dcl-S ErrorMessage varchar(1000) inz(' ');
Dcl-S outBuf char(65535) Based(p);
Dcl-S p pointer;

docNode = yajl_stmf_load_tree ( %trim(InpIFSFile) : errMsg );

If ErrMsg <> '';
ErrorMessage = 'json parse: ' + errMsg;
Else;
ContentLength = yajl_stringify( DocNode
: p
: %Size(outBuf));
EndIf;
cooldude
Posts: 15
Joined: Wed Feb 12, 2025 5:07 am

Re: Read Payload from IFS File to a string in RPGLE

Post by cooldude »

Additionally, the IFS file is with ".txt" extension.

Sample data in IFS file:
{"purchaseOrder":[{"poNumber":99999909,"branch":444,"shipVia":"","requisitionBy":" ","dateToRcv":"2025-04-24","poDetail":[{"poNumber":99999909,"seqNumber":1,"itemNumber":"ABCDEFGH","itemUOM":"PC","quantityOrdered":100,"cost":16.000},{"poNumber":99999909,"seqNumber":2,"itemNumber":"ZZZYYYAAA","itemUOM":"PC","quantityOrdered":100,"cost":36.000}]}]}

Also tried the below, which did not work either:
Dcl-S outString char(65535) Inz;
docNode = yajl_stmf_load_tree ( %trim(InpIFSFile) : errMsg );
node = YAJL_object_find(docNode: 'purchaseOrder');
outString = YAJL_get_string(node);
cooldude
Posts: 15
Joined: Wed Feb 12, 2025 5:07 am

Re: Read Payload from IFS File to a string in RPGLE

Post by cooldude »

Another related question:
The objective here is to send a payload (JSON/XML) from an IFS path (file is in .txt format) to an external API using http_url_post - for which trying to get the full payload into a string.
But can we directly send the IFS file to external API, using http_url_post or a different one? Please advise, if I can refer any example.
Thanks!
cooldude
Posts: 15
Joined: Wed Feb 12, 2025 5:07 am

Re: Read Payload from IFS File to a string in RPGLE

Post by cooldude »

All,
FYI - I was able to use QSYS2.IFS_READ table function to read data from IFS file.
jonboy49
Posts: 244
Joined: Wed Jul 28, 2021 8:18 pm

Re: Read Payload from IFS File to a string in RPGLE

Post by jonboy49 »

I know you have been working on an alternative approach - although how SQL helped I'm not sure - but as far as I can see your second approach works just fine.

Code: Select all

ctl-opt BNDDIR('YAJL')
        OPTION(*SRCSTMT:*NODEBUGIO: *NOSHOWCPY);

/copy yajl/qrpglesrc,yajl_h

Dcl-S docNode like(yajl_val);
Dcl-S node like(yajl_val);
Dcl-S outstring varchar(1000) inz('');
Dcl-S errmsg varchar(500) inz('');
Dcl-s contentLength int(10);
Dcl-s p pointer inz(%Addr(outstring));

docNode = yajl_stmf_load_tree ( '/home/paris/jsonstuff/sample1.json' : errmsg );


node = YAJL_object_find(docNode: 'purchaseOrder');

// This won't work because node is not pointing at a string 
outString = YAJL_get_string(node);

// This does work and as far as I can see does what you want.
contentLength = yajl_stringify( node : p : %Size(outstring));
YAJL-get_string was never going to work because 'purchaseOrder' is not a string. But yajl_stringify does exactly what you want as far as I can see. I copied your json to the IFS and the code above seems to extract the data you want.
Scott Klement
Site Admin
Posts: 872
Joined: Sun Jul 04, 2021 5:12 am

Re: Read Payload from IFS File to a string in RPGLE

Post by Scott Klement »

Let's start from the beginning of the discussion:
cooldude wrote: Wed Apr 16, 2025 4:48 pm Trying to read a JSON payload from an IFS file to a string. But ContentLength comes as 0. Any thoughts on what am I missing?
Can you explain what is meant, here? Content-Length has no relationship to reading a file from the IFS. So there is part of the discussion missing, here.. . are you trying to perform an HTTP request using the IFS data? That was not included at all in this message, and is where the problem is likely occurring.

In this message you did this:
cooldude wrote: Wed Apr 16, 2025 4:48 pm docNode = yajl_stmf_load_tree ( %trim(InpIFSFile) : errMsg );

If ErrMsg <> '';
ErrorMessage = 'json parse: ' + errMsg;
Else;
ContentLength = yajl_stringify( DocNode
: p
: %Size(outBuf));
EndIf;
What this does is read a file from the IFS, parse it into a JSON tree structure, and then convert the JSON tree back to JSON format, I don't know why someone would do that (it uses a lot of computing power to arrive back where you started.) But it has nothing whatsoever to do with a content-length. Indeed, there is no explanation of where you are even seeing a content-length.

In the next message:
cooldude wrote: Wed Apr 16, 2025 6:45 pm Also tried the below, which did not work either:
Dcl-S outString char(65535) Inz;
docNode = yajl_stmf_load_tree ( %trim(InpIFSFile) : errMsg );
node = YAJL_object_find(docNode: 'purchaseOrder');
outString = YAJL_get_string(node);
What is meant by "did not work"? This code reads a file, then finds the 'purchaseOrder' property in the document node. Then it tries to interpret it as a string (but it is not, it is an array of objects). What are you expecting to happen here? Are you trying to stringify just the purchaseOrder part of the document without the enclosing object?

What does this have to do with the Content-Length you mentioned at the start of the thread? Or is it a completely a completely separate issue?

Next message:
cooldude wrote: Wed Apr 16, 2025 7:13 pm Another related question:
The objective here is to send a payload (JSON/XML) from an IFS path (file is in .txt format) to an external API using http_url_post - for which trying to get the full payload into a string.
But can we directly send the IFS file to external API, using http_url_post or a different one? Please advise, if I can refer any example.
This is the first mention you've made of httpapi, you've mentioned http_url_post. But that is an old, obsolete routine. If you want the old obsolete routine to send a file from the ifs, it's http_url_post_stmf. But I would use the newer routines, http_req() (or possibly http_stmf() depending on what you want to do with the result that comes back.)

But what does this have to do with the preceding posts? They were using YAJL to interpret the JSON. This post is talking about sending a file over the internet. Two completely different things. Though, at least this message makes sense with regards to the content-length.

Next message:
cooldude wrote: Thu Apr 17, 2025 2:12 pm FYI - I was able to use QSYS2.IFS_READ table function to read data from IFS file.
This, again, leaves me very confused. This reads IFS data into a variable. It doesn't intepret the json (as per your first two posts) or send it over a network (as per your third.) If this "solved" the problem, then obviously you're doing other things with it after you read it from disk, but you don't tell us what that is.
Post Reply