If you’re getting an error that reads something like “Binder Error: Could not find key “…” in struct” when using the array_extract()
function in DuckDB, it could be that you’re specifying a non-existent key.
When using DuckDB’s array_extract()
function to extract a value from a struct, we must provide a key that actually exists in the struct. Otherwise we’ll end up with an error like the above one.
To fix this issue, be sure to specify a key that actually exists in the struct.
Example of Error
Here’s an example of code that produces the error:
SELECT array_extract(
{ 'name' : 'Fetch', 'type' : 'Dog' },
'Oops'
);
Result:
Binder Error: Could not find key "oops" in struct
Candidate Entries: "type"
Here, I passed a key called Oops
, but there’s no such key in the struct.
Sometimes this error could be due to us inadvertently passing a value instead of a key. For example:
SELECT array_extract(
{ 'name' : 'Fetch', 'type' : 'Dog' },
'Fetch'
);
Result:
Binder Error: Could not find key "fetch" in struct
Candidate Entries: "name"
Same error, although this time it suggested a different “candidate entry”. Regardless, the solution is the same.
Solution
To fix this issue, we need to specify a key that actually exists in the struct.
Example:
SELECT array_extract(
{ 'name' : 'Fetch', 'type' : 'Dog' },
'name'
);
Result:
Fetch
This time it worked. I specified the name
key, and the function returned the value at that key.