I'm afraid you are still misunderstanding what "nested" does, it will not get rid of the loop for arrays and nor it is meant to.

Have a look at this SQL, which takes a JSON object containing and array of invoices, each with a nested array of invoice lines, with each line potentially having a nested array or discount codes and extracts the contents into three corresponding database files, which is something similar to a real-world situation.
Build software better, together<>
GitHub is where people build software. More than 40 million people use GitHub to discover, fork, and contribute to over 100 million projects.

The first table is just somewhere for me to store the JSON test object with the invoices in so isn't really relevant to the rest of the discussion.

The second SQL just iterates over the array of invoices and extracts the JSON fields to the INVOICE_HEADER file. Here you can see the use of "default ... on empty" as another way of avoiding nulls in the result when a missing field is encountered (the email address being optional). The result of this SQL is one row per invoice.

The third SQL uses a nested path in order to iterate over each invoice line of each invoice, in order to extract the invoice line fields to the INVOICE_LINE file. The outer part of the path selector takes the INVOICE_NUMBER of the current invoice (used as our database key to tie everything together later), the inner, nested, part targets the array of invoice lines. The result of this SQL is one row per invoice line (for ALL invoices).

The fourth SQL uses a nested path in order to iterate over each invoice line discount, of each invoice line, of each invoice, in order to extract the discount code to the INVOICE_DISCOUNT file. Again, the outer parts of the path selector take the INVOICE_NUMBER of the current invoice and the LINE_NUMBER of the current line as the keys fields while the inner part targets the array of invoice line discounts. The result of this SQL is one row per invoice line discount (for ALL invoices and ALL invoice lines).

"strict" and "lax" are just used to indicate to the function how tolerant it should be of missing elements and badly formed JSON, so if you target "lax $.email" and it's not there, you won't get an error.

The final SQL just demonstrates how each of the three files above are linke together using the key information.


From: WEB400 <web400-bounces@xxxxxxxxxxxxxxxxxx> on behalf of Booth Martin <booth@xxxxxxxxxxxx>
Sent: 20 November 2019 19:41
To: Web Enabling the IBM i (AS/400 and iSeries) <web400@xxxxxxxxxxxxxxxxxx>
Subject: Re: [WEB400] json_table(httpgetclob, nested array

I am plowing through this. Stubbornness & obstinence get in my way, so
it takes time.

It does look like your example is the piece that I needed to tie it all
together; thank you. I do have a working 5250 application that consumes
and displays the weather in Moscow, Kiev, and London. It uses the loop,
which I believe "nested" replaces, but gads... worrying abut that is
days away at the rates I make progress.

incidentally, lines of code is not a metric for me, but I need to start
with Hello World levels and build on that or I gloss over the
is-it-plugged-in stupids

On 11/20/2019 3:28 AM, Tim Fathers wrote:
Booth, I'm afraid I'm at a loss then as to what it is you're trying to ask or what the problem is with the solutions you've been provided.
This is the Web Enabling the IBM i (AS/400 and iSeries) (WEB400) mailing list
To post a message email: WEB400@xxxxxxxxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
or email: WEB400-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives

As an Amazon Associate we earn from qualifying purchases.

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2022 by and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.