skip to Main Content
{
    "general": {
      "product_key": "501088",
      "group_subtype_id": 1,
      "group_subtype_name": "Wheel Speed Sensor",
      "variant_id": 6,
      "variant_name": "DF22",
      "rb_customer_id": 287383
    },
    "partnumbers": [
      {
        "partnumber": "F04FD009BD",
        "pn_type": "Series OEM",
        "mat_status": "00 - planned",
        "properties": [
          {"property_id":4,"property_name":"ASIC P/N","value_id":38,"value":"8905502648"},
          {"property_id":5,"property_name":"ASIC type","value_id":56,"value":"TLE4942"},
          {"property_id":6,"property_name":"Axle","value_id":62,"value":"Front / Rear Right"},
          {"property_id":7,"property_name":"Base Type - Development P/N","value_id":72,"value":"FFF"},
          {"property_id":8,"property_name":"Base Type - Released P/N","value_id":73,"value":"SSS"}
        ]
      }
    ],
    "document_numbers": [
      {"document_number":"1234569871","document_version":"05","document_type":"TCD"},
      {"document_number":"0123456789","document_version":"01","document_type":"TCD"},
      {"document_number":"1234569870","document_version":"05","document_type":"TCD"},
      {"document_number":"1234567890","document_version":"01","document_type":"TCD"}
    ],
    "target_markets": [
      {"country_name":"Belize","iso_code":"BZ"},
      {"country_name":"Central African Republic","iso_code":"CF"},
      {"country_name":"Albania","iso_code":"AL"}
    ]
  }

Need this output :
enter image description here

I tried using nested path , but not getting expected output

2

Answers


  1. This is a bit complicated cause your json sample data. For 1 "general" row there is 1 "partnumbers" row having 5 "properties" rows which are transposed to columns. For the same 1 "general" row there are 4 rows for "document_numbers" and 3 rows for "target_markets" (transformed to list). Since you want 4 rows of data in your expected result you should further process (Main SQL) the data fetched (grid cte) from your JSON response.
    Comments in code too…

    WITH      --  S a m p l e    D a t a :     (json)
        jst AS
            ( Select 
    '{
        "general": {
          "product_key": "501088",
          "group_subtype_id": 1,
          "group_subtype_name": "Wheel Speed Sensor",
          "variant_id": 6,
          "variant_name": "DF22",
          "rb_customer_id": 287383
        },
        "partnumbers": [
          {
            "partnumber": "F04FD009BD",
            "pn_type": "Series OEM",
            "mat_status": "00 - planned",
            "properties": [
              {"property_id":4,"property_name":"ASIC P/N","value_id":38,"value":"8905502648"},
              {"property_id":5,"property_name":"ASIC type","value_id":56,"value":"TLE4942"},
              {"property_id":6,"property_name":"Axle","value_id":62,"value":"Front / Rear Right"},
              {"property_id":7,"property_name":"Base Type - Development P/N","value_id":72,"value":"FFF"},
              {"property_id":8,"property_name":"Base Type - Released P/N","value_id":73,"value":"SSS"}
            ]
          }
        ],
        "document_numbers": [
          {"document_number":"1234569871","document_version":"05","document_type":"TCD"},
          {"document_number":"0123456789","document_version":"01","document_type":"TCD"},
          {"document_number":"1234569870","document_version":"05","document_type":"TCD"},
          {"document_number":"1234567890","document_version":"01","document_type":"TCD"}
        ],
        "target_markets": [
          {"country_name":"Belize","iso_code":"BZ"},
          {"country_name":"Central African Republic","iso_code":"CF"},
          {"country_name":"Albania","iso_code":"AL"}
        ]
      }'  as JSTXT  From Dual),
    

    Use JSON_TABLE() to fetch rows and columns from your JSON response string:
    … create a cte (named it grid)

        grid AS
            ( Select  x.PRODUCT_KEY, x.GROUP_SUB_TYPE_ID, 
                      x.GROUP_SUB_TYPE_NAME "GROUP_SUB_TYPE_NAME", x.VARIANT_ID "VARIANT_ID", x.VARIANT_NAME "VARIANT_NAME", x.RB_CUSTOMER "RB_CUSTOMER",
                      x.PARTNUMBER "PARTNUMBER", x.PN_TYPE "PN_TYPE", x.MAT_STATUS "MAT_STATUS", 
                      --  use Case expressions to get properties as columns instead of rows
                      Case When x.PROPERTY_ID = 4 Then x.PROPERTY_NAME || ': ' || x.VALUE End "PROPERTY_4", 
                      Case When x.PROPERTY_ID = 5 Then x.PROPERTY_NAME || ': ' || x.VALUE End "PROPERTY_5", 
                      Case When x.PROPERTY_ID = 6 Then x.PROPERTY_NAME || ': ' || x.VALUE End "PROPERTY_6", 
                      Case When x.PROPERTY_ID = 7 Then x.PROPERTY_NAME || ': ' || x.VALUE End "PROPERTY_7", 
                      Case When x.PROPERTY_ID = 8 Then x.PROPERTY_NAME || ': ' || x.VALUE End "PROPERTY_8", 
                      --
                      x.DOCUMENT_NUMBER, x.DOCUMENT_VERSION, x.DOCUMENT_TYPE,
                      -- use LISTAGG() to get lists (Country names and ISO codes)
                      LISTAGG(Distinct x.COUNTRY_NAME, ':') WITHIN GROUP (Order By x.COUNTRY_NAME) Over(Partition By x.PRODUCT_KEY, x.GROUP_SUB_TYPE_ID, x.DOCUMENT_NUMBER, x.DOCUMENT_VERSION, x.DOCUMENT_TYPE) "COUNTRY_NAME", 
                      LISTAGG(Distinct x.ISO_CODE, ':') WITHIN GROUP (Order By x.ISO_CODE) Over(Partition By x.PRODUCT_KEY, x.GROUP_SUB_TYPE_ID, x.DOCUMENT_NUMBER, x.DOCUMENT_VERSION, x.DOCUMENT_TYPE) "ISO_CODE"
              From    jst j, 
                      JSON_TABLE( j.JSTXT, '$'
                       COLUMNS ( PRODUCT_KEY           VARCHAR2(12)     PATH '$.general.product_key',
                                 GROUP_SUB_TYPE_ID     NUMBER       PATH '$.general.group_subtype_id',
                                 GROUP_SUB_TYPE_NAME   VARCHAR2(24)     PATH '$.general.group_subtype_name',
                                 VARIANT_ID            NUMBER       PATH '$.general.variant_id',
                                 VARIANT_NAME          VARCHAR2(16)     PATH '$.general.variant_name',
                                 RB_CUSTOMER           NUMBER       PATH '$.general.rb_customer_id',
                                   NESTED '$.partnumbers[*]'
                                      COLUMNS ( PARTNUMBER      VARCHAR2(12)   PATH '$.partnumber',
                                                PN_TYPE         VARCHAR2(12)   PATH '$.pn_type',
                                                MAT_STATUS      VARCHAR2(12)   PATH '$.mat_status',
                                                    NESTED '$.properties[*]'
                                                        COLUMNS ( PROPERTY_ID     VARCHAR2(16)   PATH '$.property_id',
                                                                  PROPERTY_NAME   VARCHAR2(32)   PATH '$.property_name',
                                                                  VALUE_ID        NUMBER(8)      PATH '$.value_id',
                                                                  VALUE           VARCHAR2(24)   PATH '$.value'
                                                                )
                                              ),
                                  NESTED '$.document_numbers[*]'
                                      COLUMNS ( DOCUMENT_NUMBER  VARCHAR2(12)    PATH '$.document_number', 
                                                DOCUMENT_VERSION VARCHAR2(12)    PATH '$.document_version',
                                                DOCUMENT_TYPE    VARCHAR2(12)    PATH '$.document_type'
                                              ), 
                                  NESTED '$.target_markets[*]'
                                      COLUMNS ( COUNTRY_NAME  VARCHAR2(30)    PATH '$.country_name',
                                                ISO_CODE      VARCHAR2(2)     PATH '$.iso_code'
                                              )
                              )
                          ) x
          )
    

    … Process resultset to get ridd of duplicates and to format your expected result :

    --      M a i n    S Q L :
    --  self joined grid cte to format your expected result
    SELECT  g.PRODUCT_KEY, g.GROUP_SUB_TYPE_NAME, g.VARIANT_ID, g.VARIANT_NAME,  g.RB_CUSTOMER, 
            Max(g.PARTNUMBER) "PARTNUMBER", Max(g.PN_TYPE) "PN_TYPE", 
            Max(g.PROPERTY_4) "PROPERTY_4", Max(g.PROPERTY_5) "PROPERTY_5", Max(g.PROPERTY_6) "PROPERTY_6", Max(g.PROPERTY_7) "PROPERTY_7", Max(g.PROPERTY_8) "PROPERTY_8",
            doc.DOCUMENT_NUMBER, doc.DOCUMENT_VERSION, doc.DOCUMENT_TYPE,
            Max(g.COUNTRY_NAME) "COUNTRY_NAME", Max(g.ISO_CODE) "ISO_CODE"
    FROM grid g
    INNER JOIN grid doc ON(doc.PRODUCT_KEY = doc.PRODUCT_KEY And doc.DOCUMENT_NUMBER Is Not Null And g.GROUP_SUB_TYPE_ID = doc.GROUP_SUB_TYPE_ID)
    WHERE g.DOCUMENT_NUMBER Is Null
    GROUP BY    g.PRODUCT_KEY, g.GROUP_SUB_TYPE_NAME, g.VARIANT_ID, g.VARIANT_NAME, g.RB_CUSTOMER,
                doc.DOCUMENT_NUMBER, doc.DOCUMENT_VERSION, doc.DOCUMENT_TYPE
    
    /*      R e s u l t :
    PRODUCT_KEY  GROUP_SUB_TYPE_NAME      VARIANT_ID VARIANT_NAME     RB_CUSTOMER PARTNUMBER   PN_TYPE      PROPERTY_4                                                 PROPERTY_5                                                 PROPERTY_6                                                 PROPERTY_7                                                 PROPERTY_8                                                 DOCUMENT_NUM DOCUMENT_VER DOCUMENT_TYP COUNTRY_NAME                               ISO_CODE    
    ------------ ------------------------ ---------- ---------------- ----------- ------------ ------------ ---------------------------------------------------------- ---------------------------------------------------------- ---------------------------------------------------------- ---------------------------------------------------------- ---------------------------------------------------------- ------------ ------------ ------------ ------------------------------------------ ------------
    501088       Wheel Speed Sensor                6 DF22                  287383 F04FD009BD   Series OEM   ASIC P/N: 8905502648                                       ASIC type: TLE4942                                         Axle: Front / Rear Right                                   Base Type - Development P/N: FFF                           Base Type - Released P/N: SSS                              0123456789   01           TCD          Albania:Belize:Central African Republic    AL:BZ:CF    
    501088       Wheel Speed Sensor                6 DF22                  287383 F04FD009BD   Series OEM   ASIC P/N: 8905502648                                       ASIC type: TLE4942                                         Axle: Front / Rear Right                                   Base Type - Development P/N: FFF                           Base Type - Released P/N: SSS                              1234567890   01           TCD          Albania:Belize:Central African Republic    AL:BZ:CF    
    501088       Wheel Speed Sensor                6 DF22                  287383 F04FD009BD   Series OEM   ASIC P/N: 8905502648                                       ASIC type: TLE4942                                         Axle: Front / Rear Right                                   Base Type - Development P/N: FFF                           Base Type - Released P/N: SSS                              1234569870   05           TCD          Albania:Belize:Central African Republic    AL:BZ:CF    
    501088       Wheel Speed Sensor                6 DF22                  287383 F04FD009BD   Series OEM   ASIC P/N: 8905502648                                       ASIC type: TLE4942                                         Axle: Front / Rear Right                                   Base Type - Development P/N: FFF                           Base Type - Released P/N: SSS                              1234569871   05           TCD          Albania:Belize:Central African Republic    AL:BZ:CF    */
    
    Login or Signup to reply.
  2. Parse each of the nested JSON structures separately not using NESTED PATH in the initial JSON_TABLE and returning the arrays using FORMAT JSON and then you can use correlated sub-queries (using CROSS APPLY or CROSS JOIN LATERAL) to parse the individual queries and cross join the rows in each of the arrays.

    The part numbers can be generated using a filter expression in the JSON path (rather than rows for each property and then pivoting). The target markets can be generated and aggregated so that a single row is always generated.

    Like this:

    SELECT jt.product_key,
           jt.group_subtype_id,
           jt.group_subtype_name,
           jt.variant_id,
           jt.variant_name,
           jt.rb_customer_id,
           pn.partnumber,
           pn.pn_type,
           pn.mat_status,
           pn.property_id_4_name || ':' || pn.property_id_4_value AS property_id_4,
           pn.property_id_5_name || ':' || pn.property_id_5_value AS property_id_5,
           pn.property_id_6_name || ':' || pn.property_id_6_value AS property_id_6,
           pn.property_id_7_name || ':' || pn.property_id_7_value AS property_id_7,
           pn.property_id_8_name || ':' || pn.property_id_8_value AS property_id_8,
           dn.document_number,
           dn.document_version,
           dn.document_type,
           tm.country_name,
           tm.iso_code,
           hi.child_product
    FROM   (
             SELECT jt.*
             FROM   table_name t
                    CROSS APPLY JSON_TABLE(
                      t.value,
                      '$'
                      COLUMNS (
                        product_key           NUMBER        PATH '$.general.product_key',
                        group_subtype_id      NUMBER        PATH '$.general.group_subtype_id',
                        group_subtype_name    VARCHAR2(100) PATH '$.general.group_subtype_name',
                        variant_id            NUMBER        PATH '$.general.variant_id',
                        variant_name          VARCHAR2(100) PATH '$.general.variant_name',
                        rb_customer_id        NUMBER        PATH '$.general.rb_customer_id',
                        partnumbers           CLOB FORMAT JSON PATH '$.partnumbers',
                        document_numbers      CLOB FORMAT JSON PATH '$.document_numbers',
                        target_markets        CLOB FORMAT JSON PATH '$.target_markets',
                        hierarchy_information CLOB FORMAT JSON PATH '$.hierarchy_information'
                      )
                    ) jt
             WHERE  ROWNUM > 0
           ) jt
           CROSS APPLY JSON_TABLE(
             jt.partnumbers,
             '$[*]'
             COLUMNS (
               partnumber          VARCHAR2(100) PATH '$.partnumber',
               pn_type             VARCHAR2(100) PATH '$.pn_type',
               mat_status          VARCHAR2(100) PATH '$.mat_status',
               property_id_4_name  VARCHAR2(100) PATH '$.properties[*]?(@.property_id=="4").property_name',
               property_id_4_value VARCHAR2(100) PATH '$.properties[*]?(@.property_id=="4").value',
               property_id_5_name  VARCHAR2(100) PATH '$.properties[*]?(@.property_id=="5").property_name',
               property_id_5_value VARCHAR2(100) PATH '$.properties[*]?(@.property_id=="5").value',
               property_id_6_name  VARCHAR2(100) PATH '$.properties[*]?(@.property_id=="6").property_name',
               property_id_6_value VARCHAR2(100) PATH '$.properties[*]?(@.property_id=="6").value',
               property_id_7_name  VARCHAR2(100) PATH '$.properties[*]?(@.property_id=="7").property_name',
               property_id_7_value VARCHAR2(100) PATH '$.properties[*]?(@.property_id=="7").value',
               property_id_8_name  VARCHAR2(100) PATH '$.properties[*]?(@.property_id=="8").property_name',
               property_id_8_value VARCHAR2(100) PATH '$.properties[*]?(@.property_id=="8").value'
             )
           ) pn
           CROSS APPLY JSON_TABLE(
             jt.document_numbers,
             '$[*]' COLUMNS (
                 document_number  VARCHAR2(100) PATH '$.document_number',
                 document_version VARCHAR2(100) PATH '$.document_version',
                 document_type    VARCHAR2(100) PATH '$.document_type'
               )
           ) dn
           CROSS JOIN LATERAL (
             SELECT LISTAGG(country_name, ':') AS country_name,
                    LISTAGG(iso_code, ':') AS iso_code
             FROM   JSON_TABLE(
                      jt.target_markets,
                      '$[*]'
                      COLUMNS (
                        country_name VARCHAR2(100) PATH '$.country_name',
                        iso_code     VARCHAR2(3)   PATH '$.iso_code'
                      )
                    )
           ) tm
           OUTER APPLY JSON_TABLE(
             jt.hierarchy_information,
             '$[*]' COLUMNS (
                 child_product    VARCHAR2(100) PATH '$'
               )
           ) hi
    

    Note: WHERE ROWNUM > 0 looks like it does nothing as it will always be true but it is used to materialise the initial query which allows you to chain the correlated JSON_TABLEs.

    Which, for the sample data:

    CREATE TABLE table_name (value) AS
      SELECT EMPTY_CLOB() || '{
        "general": {
          "product_key": "501088",
          "group_subtype_id": 1,
          "group_subtype_name": "Wheel Speed Sensor",
          "variant_id": 6,
          "variant_name": "DF22",
          "rb_customer_id": 287383
        },
        "partnumbers": [
          {
            "partnumber": "F04FD009BD",
            "pn_type": "Series OEM",
            "mat_status": "00 - planned",
            "properties": [
              {"property_id":4,"property_name":"ASIC P/N","value_id":38,"value":"8905502648"},
              {"property_id":5,"property_name":"ASIC type","value_id":56,"value":"TLE4942"},
              {"property_id":6,"property_name":"Axle","value_id":62,"value":"Front / Rear Right"},
              {"property_id":7,"property_name":"Base Type - Development P/N","value_id":72,"value":"FFF"},
              {"property_id":8,"property_name":"Base Type - Released P/N","value_id":73,"value":"SSS"}
            ]
          }
        ],
        "document_numbers": [
          {"document_number":"1234569871","document_version":"05","document_type":"TCD"},
          {"document_number":"0123456789","document_version":"01","document_type":"TCD"},
          {"document_number":"1234569870","document_version":"05","document_type":"TCD"},
          {"document_number":"1234567890","document_version":"01","document_type":"TCD"}
        ],
        "target_markets": [
          {"country_name":"Belize","iso_code":"BZ"},
          {"country_name":"Central African Republic","iso_code":"CF"},
          {"country_name":"Albania","iso_code":"AL"}
        ]
      }'
      FROM DUAL;
    

    Outputs:

    PRODUCT_KEY GROUP_SUBTYPE_ID GROUP_SUBTYPE_NAME VARIANT_ID VARIANT_NAME RB_CUSTOMER_ID PARTNUMBER PN_TYPE MAT_STATUS PROPERTY_ID_4 PROPERTY_ID_5 PROPERTY_ID_6 PROPERTY_ID_7 PROPERTY_ID_8 DOCUMENT_NUMBER DOCUMENT_VERSION DOCUMENT_TYPE COUNTRY_NAME ISO_CODE CHILD_PRODUCT
    501088 1 Wheel Speed Sensor 6 DF22 287383 F04FD009BD Series OEM 00 – planned ASIC P/N:8905502648 ASIC type:TLE4942 Axle:Front / Rear Right Base Type – Development P/N:FFF Base Type – Released P/N:SSS 1234569871 05 TCD Belize:Central African Republic:Albania BZ:CF:AL null
    501088 1 Wheel Speed Sensor 6 DF22 287383 F04FD009BD Series OEM 00 – planned ASIC P/N:8905502648 ASIC type:TLE4942 Axle:Front / Rear Right Base Type – Development P/N:FFF Base Type – Released P/N:SSS 0123456789 01 TCD Belize:Central African Republic:Albania BZ:CF:AL null
    501088 1 Wheel Speed Sensor 6 DF22 287383 F04FD009BD Series OEM 00 – planned ASIC P/N:8905502648 ASIC type:TLE4942 Axle:Front / Rear Right Base Type – Development P/N:FFF Base Type – Released P/N:SSS 1234569870 05 TCD Belize:Central African Republic:Albania BZ:CF:AL null
    501088 1 Wheel Speed Sensor 6 DF22 287383 F04FD009BD Series OEM 00 – planned ASIC P/N:8905502648 ASIC type:TLE4942 Axle:Front / Rear Right Base Type – Development P/N:FFF Base Type – Released P/N:SSS 1234567890 01 TCD Belize:Central African Republic:Albania BZ:CF:AL null

    fiddle

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search