I’m currently struggling to import my API-Response to my Oracle table.
I’m using the JSON_TABLE()
but this function is not working as expected.
I have a JSON value like this:
{
"CAR-1": [
{
"state": "waiting",
"timestamp": 1720476000
}
],
"CAR-13": [
{
"state": "delivery",
"timestamp": 1720476000
}
],
"CAR-15": [
{
"state": "pickup",
"timestamp": 1720476000
}
],
"CAR-16": [
{
"state": "waiting",
"timestamp": 1720476000
}
],
"CAR-19": [
{
"state": "waiting",
"timestamp": 1720476000
}
],
"CAR-2": [
{
"state": "waiting",
"timestamp": 1720476000
}
]
}...
hint: there can be several entries per car if I increase the time range.
Currently I use the following plsql to extract the data into my db table:
INSERT INTO car_event_table (car_name, event_state, event_timestamp)
SELECT jt.car_name, jt.event_state, jt.event_timestamp
FROM JSON_TABLE(l_response,
'$.*[*]'
COLUMNS(car_name VARCHAR2 PATH '$',
event_state VARCHAR2 PATH '$.state',
event_timestamp NUMBER PATH '$.timestamp')) jt;
Unfortunately I cannot extract the car_name from the json object.
Can someone help me?
2
Answers
Oracle does not have a built-in way to dynamically get keys from an object. However, you can create a function that can retrieve them for you if you know their position within the JSON document:
Then you can use the query:
Which, if you have the table:
and
l_response
is:Then, after the
INSERT
, the table contains:fiddle
In fact you can do it without creating a function, but with version >= 21c only: