Re: Using DATA-GEN to generate JSON

Jon Paris <[email protected]>
Newsgroups gmane.comp.lang.as400.rpg
Message-ID <[email protected]>
Not sure what the heck YAGL is Darryl - did you mean YAJL?

Most people use Scott's generator.  Where did you get this one?

Regardless, you need to use countprefix to correctly output arrays. By "correctly", I mean only outputting the number of elements that have been filled. BUT outputting empty elements should not cause a crash, regardless.

As always, it would be helpful if you had actually included a sample of the DS and Data-Gen you are using.

This is roughly what it should look like.

Dcl-ds  Invoice Qualified Inz;
   CustomerId       char(6);
   Invoice_Number   int(10);
   count_Invoice_Detail  int(5);
   Dcl-ds Invoice_Detail Dim(20);
      Item  char(10);
      Quantity  int(5);
      ...
   End-ds;
End-ds;

Then the DG would include this:

%Data( json : 'doc=file countprefix=count_ ')


Be aware, though that this may not answer all of your problems. If I recall correctly, one of the issues with DG is that it will output an empty array when the count is zero.

e.g. if count_Invoice_Detail = zero the output would include:

... { "Invoice_Detail" : [ ] } or something like that.

You can see an example that uses countprefix for an optional field in this article https://www.itjungle.com/2021/07/12/guru-web-services-data-into-and-data-gen-part-3/. 

For documents that include optional blocks, you will need to use DG's *Start and *End options and output the json in pieces.

Alternatively, for highly complex documents with a lot of optional "bits", you may need to use the raw APIs in YAJL.



Jon Paris
[email protected]



> On Sep 11, 2025, at 8:30 PM, <[email protected]> <[email protected]> wrote:
> 
> No, I did not see parameters for those.
> 
> Darryl Freinkel
> 
> -- 
> This is the RPG programming on IBM i (RPG400-L) mailing list
> To post a message email: [email protected]
> To subscribe, unsubscribe, or change list options,
> visit: https://lists.midrange.com/mailman/listinfo/rpg400-l
> or email: [email protected]
> Before posting, please take a moment to review the archives
> at https://archive.midrange.com/rpg400-l.
> 
> Please contact [email protected] for any subscription related questions.
> 

-- 
This is the RPG programming on IBM i (RPG400-L) mailing list
To post a message email: [email protected]
To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/rpg400-l
or email: [email protected]
Before posting, please take a moment to review the archives
at https://archive.midrange.com/rpg400-l.

Please contact [email protected] for any subscription related questions.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.