-3

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.

2
  • For representation , i have mentioned 100, but tried 1000 as well and still the same problem. Commented May 21, 2021 at 10:55
  • db<>fiddle Commented May 21, 2021 at 11:01

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.