select JT.*
FROM JSON_TABLE ('{
"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"}
]
}',
'$'
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',
NESTED PATH '$.partnumbers[*]' COLUMNS (
partnumber VARCHAR2 ( 100 ) PATH '$.partnumber',
pn_type VARCHAR2 ( 100 ) PATH '$.pn_type',
mat_status VARCHAR2 ( 100 ) PATH '$.mat_status',
NESTED PATH '$.properties[*]' COLUMNS (
property_id VARCHAR2 ( 100 ) PATH '$.property_id',
property_name VARCHAR2 ( 100 ) PATH '$.property_name',
value_id VARCHAR2 ( 100 ) PATH '$.value_id',
value VARCHAR2 ( 100 ) PATH '$.value',
NESTED PATH '$.document_numbers[*]' COLUMNS (
document_number VARCHAR2 ( 100 ) PATH '$.document_number',
document_verion NUMBER PATH '$.document_verion',
document_type VARCHAR2 ( 100 ) PATH '$.document_type',
NESTED PATH '$.target_markets[*]' COLUMNS (
country_name VARCHAR2 ( 100 ) PATH '$.country_name',
iso_code VARCHAR2 ( 10 ) PATH '$.iso_code',
NESTED PATH '$.hierarchy_information[*]' COLUMNS (
child_product NUMBER PATH '$'
)))))))
jt
Output:
Tried using Nested Path , but the Document numbers and Target MArkets cant be retrieved.
Kindly support,
2
Answers
Your nested path expressions are nested – that means you’re looking for the document_numbers attribute within the properties node. With some formatting that becomes clear. To get the attributes of document_numbers and target_markets, make this small change:
Note that this will give you more rows, since there is no relationship between the elements of the individual objects.
Parse each of the nested JSON structures separately not using
NESTED PATH
in the initialJSON_TABLE
and returning the arrays usingFORMAT JSON
and then you can use correlated sub-queries (usingCROSS APPLY
orCROSS 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:
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 correlatedJSON_TABLE
s.Which, for the sample data:
Outputs:
fiddle