Part 3: Parsing Complex JSON Objects with DB2 SQL

Part 3: Parsing Complex JSON Objects with DB2 SQL

So this credit goes to Birgitta Hauser , I had e-mailed her prior to posting the last two articles asking for some help as I had spent a couple of days trying to get everything working and she provided this example which is probably the easiest to understand of all of them. I just like to post these mainly for my reference but maybe it will help someone in the future. Thanks Birgitta for always contributing to the IBMi community.

So again we are going to put our JSON string into a table like before. This is to make the SQL easier to read.

-- create, clear, insert json string into a table to hold our json information
Create Or Replace Table rthompson.t_json (jsonstring Clob(5000));
Delete from rthompson.t_json;
Insert Into rthompson.t_json
  Values
    (
'{"data":[{"order":[{"order_number":"180","order_date":"2023-01-01","orderdetail":[{"sequence":1,"product_number":"ABC","quantity":10,"options":[{"color1":"blue","color2":"white"}]},{"sequence":2,"product_number":"DEF","quantity":5,"options":[{"color1":"green","color2":"yellow"}]}]},{"order_number":"181","order_date":"2023-02-01","orderdetail":[{"sequence":1,"product_number":"RLT","quantity":10,"options":[{"color1":"cyan","color2":"black"}]},{"sequence":2,"product_number":"XYZ","quantity":5,"options":[{"color1":"purple","color2":"gray"}]}]}]}]}'
    );        

Instead of using the UNNEST table function that we built previously we will use a SQL keyword called Nested to basically unnest the other arrays.

Also she explained that you don't have to specific the path if you use the same names as in the JSON, but you can if needed and I provided one example of that for order_number. Below would be an example parsing the above JSON object and having a single row for the Order/Detail Line/Options.

Select x.*
  From Json_Table(
      (Select *
          From rthompson.t_json
          Fetch First Row Only),
      '$.data.order[*]'
      Columns(
        "order_number" Integer path '$.order_number'
        ,"order_date" Date
        ,Nested '$.orderdetail[*]' Columns(
          "sequence" Integer
          ,"product_number" Char(20)
          ,"quantity" Integer
          ,Nested '$.options[*]' Columns(
            "color1" Char(10)
            ,"color2" Char(10)
          )
        )
      )
    ) As x;        

Hope this helps someone in the future.



Brian Bunney

IBM i Software Developer

1 年

Unless I was a hard core SQL developer why would I want to do all this in SQL when I can do it with a single operation in RPGLE using Data-Into with a parser like YAJLInto?

回复

要查看或添加评论,请登录

Ricky Thompson的更多文章

  • Splitting a PDF with RPG and Java PDFBox based on Text

    Splitting a PDF with RPG and Java PDFBox based on Text

    So this is a follow up article to my previous example of just splitting each page of a PDF document. See the previous…

  • Splitting a PDF with RPG and Java PDFBox

    Splitting a PDF with RPG and Java PDFBox

    I saw a post on Facebook asking if there was a way to split a PDF. While the poster didn't specify how he was splitting…

    9 条评论
  • RPG End Code Blocks - VS Code Extension

    RPG End Code Blocks - VS Code Extension

    I have been trying to use VS Code - Code For IBMi more to get used to it. One of the things I missed in VS Code vs RDi…

    8 条评论
  • DB2 HTTP_POST

    DB2 HTTP_POST

    In the past when I was trying to learn how to use some webservices and there was a site that basically allowed you to…

    3 条评论
  • Using SQL Lateral

    Using SQL Lateral

    I think we have probably all seen a table that we wish was designed a little differently. Like lets take a…

    3 条评论
  • Part 2: Parsing Complex JSON Objects with DB2 SQL

    Part 2: Parsing Complex JSON Objects with DB2 SQL

    In the previous article, it showed how to parse a complex JSON article but I wanted to add how you might process that…

    1 条评论
  • Parsing a Complex JSON with DB2

    Parsing a Complex JSON with DB2

    I recently needed to parse a pretty complex JSON object with DB2 SQL. This isn't the easiest thing in the world to do.

    5 条评论
  • ACS - Run SQL Scripts - Insert From Examples

    ACS - Run SQL Scripts - Insert From Examples

    I'm forever saving SQL Scripts from ACS to a PC file. Not that this is a bad habit or anything but finding them for…

    2 条评论
  • Generating the SQL DDL from an existing table

    Generating the SQL DDL from an existing table

    Every so often I see someone ask how they can generate the DDL from a Table, PF, View, or Procedure . Maybe the source…

    2 条评论
  • ILEastic / JQuery

    ILEastic / JQuery

    Do you remember the days of getting those AS/400 magazines that would have code in them that you could actually use…

    5 条评论

社区洞察

其他会员也浏览了