This answer is for the simple JSON type.
PLSQL looping through JSON object
I have a complex JSON type how do i loop thru a data like this. Is there any oracle function which traverses this complex type?
[[{"PONumber":1,"ItemNumber":1,"Part":{"Description":"Tora! Tora! Tora!","UnitPrice":19.95,"UPCCode":24543013174},"Quantity":2.0},{"ItemNumber":2,"Part":{"Description":"The Beastmaster","UnitPrice":19.95,"UPCCode":13131201598},"Quantity":4.0},{"ItemNumber":3,"Part":{"Description":"Heavy Traffic","UnitPrice":19.95,"UPCCode":27616852854},"Quantity":6.0}]]
I was able to use the following function but it doesn’t give a JSON string as output.
SELECT po.PONumber,
JSON_QUERY(jt.data, '$.LineItems.ItemNumber' WITH WRAPPER) AS Line_itemnr,
JSON_QUERY(jt.data, '$.LineItems.Part.Description' WITH WRAPPER) AS Line_itemdesc
FROM json_documents;
PONumber | LINE_ITEMNR | Line_itemdesc |
---|---|---|
[1] | [1,2,3] | ["Tora! Tora! Tora!","The Beastmaster","Heavy Traffic"] |
I want an answer like this
PONumber | LINE_ITEMNR | Description……. | Rest of the columns here |
---|---|---|---|
1 | 1 | Tora! Tora! Tora! | |
1 | 2 | The Beastmaster | |
1 | 3 | Heavy Traffic |
Thanks in advance for your response.
2
Answers
Your question isn’t really all that clear about what you’re trying to get out. You’re not even very clear about whether you’re searching for a SQL solution, or a PL/SQL solution.
However – going by the heading of your post (which mentions PL/SQL), I’m going to take a punt at the following solution. You could easily wrap this in a function and modify it to your needs.
— edit —
question appears to have been updates since I posted this.
You can use
JSON_TABLE
in a query:Which, for the sample data:
Outputs:
Or:
Which outputs:
fiddle