Skip to content
Advertisement

Asking for help on correct way to us SQL with CTE to create JSON_OBJECT

The requested JSON needs to be in this form:

The program that I created looks like this:

This is the compile error I get:

SQL0122: Position 41 Column OBJHEADER or expression in SELECT list not valid.

I am asking if this is the correct way to create this SQL statement, is there a better easier way? Any idea how to rewrite the SQL statement to make it work correctly?

Advertisement

Answer

The key with generating JSON or XML for that matter is to start from the inside and work your way out.

(I’ve simplified the raw data into just a test table…)

Now add the next layer as a CTE the builds on the first CTE..

And the next layer…

Nice thing about building with CTE’s is at each step, you can see the results so far…

You can actually always go back and stick a Select * from CTE; in the middle to see what you have at some point.

Note that I’m building this in Run SQL Scripts. Once you have the statement complete, you can embed it in your RPG program.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement