I would like to know if there is a way i could get the size of the array of a specific node in a JSON data.
As an example, below is the JSON.
For the node 'Phone' there are two items. I would like to know the size of this node. My objective is to parse the Java Array to a Java Array. If i am not aware of the size of the array i would not be able to parse.
Essentially i would like to get the value "2".
{"PONumber" : 1600,
"Reference" : "ABULL-20140421",
"Requestor" : "Alexis Bull",
"User" : "ABULL",
"CostCenter" : "A50",
"ShippingInstructions" : {"name" : "Alexis Bull",
"Address": {"street" : "200 Sporting Green",
"city" : "South San Francisco",
"state" : "CA",
"zipCode" : 99236,
"country" : "United States of America"},
"Phone" : [{"type" : "Office", "number" : "909-555-7307"},
{"type" : "Mobile", "number" : "415-555-1234"}]},
"Special Instructions" : null,
"AllowPartialShipment" : true,
"LineItems" : [{"ItemNumber" : 1,
"Part" : {"Description" : "One Magic Christmas",
"UnitPrice" : 19.95,
"UPCCode" : 13131092899},
"Quantity" : 9.0},
{"ItemNumber" : 2,
"Part" : {"Description" : "Lethal Weapon",
"UnitPrice" : 19.95,
"UPCCode" : 85391628927},
"Quantity" : 5.0}]}
As an alternative, Using the below code i would get that data:
SELECT jt.phones
FROM j_purchaseorder,
JSON_TABLE(po_document, '$.ShippingInstructions'
COLUMNS
(phones VARCHAR2(100) FORMAT JSON PATH '$.Phone')) AS jt;
however if i use
SELECT jt.phones
FROM j_purchaseorder,
JSON_TABLE(po_document, '$'
COLUMNS
(ShippingInstructions VARCHAR2(100) FORMAT JSON PATH '$.ShippingInstructions')) AS jt;
i am getting the value as null . So how can i get the entire ShippingInstructions in a single value.
For the node 'Phone' there are two items. I would like to know the size of this node.
Essentially i would like to get the value "2".
From this question and this question , you can use:
SELECT jt.phones
FROM j_purchaseorder,
JSON_TABLE(
po_document,
'$.ShippingInstructions'
COLUMNS (
phones VARCHAR2(100) FORMAT JSON WITH WRAPPER PATH '$.Phone.size()'
)
) AS jt;
Which outputs:
PHONES [2]
If you do not want the array wrapper then you can pass the return value through JSON_VALUE
:
SELECT JSON_VALUE(jt.phones, '$[0]') AS phones
FROM j_purchaseorder,
JSON_TABLE(
po_document,
'$.ShippingInstructions'
COLUMNS (
phones VARCHAR2(100) FORMAT JSON WITH WRAPPER PATH '$.Phone.size()'
)
) AS jt;
Which outputs:
PHONES 2
If you are Oracle 19c and your column is defined with an IS JSON
check constraint then you can simplify the query to:
SELECT j.po_document."ShippingInstructions"."Phone".size() phones
FROM j_purchaseorder j;
Which outputs:
PHONES 2
If you are using an early Oracle version that does not support the size()
function then you can get all rows and use COUNT
:
SELECT COUNT(*) AS phones
FROM j_purchaseorder,
JSON_TABLE(
po_document,
'$.ShippingInstructions.Phone[*]'
COLUMNS (
phones VARCHAR2(100) FORMAT JSON PATH '$'
)
) AS jt;
Which outputs:
PHONES 2
db<>fiddle here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.