Considering the following content on a jsonb field, and that the keys immediately after "audio" are random (I’ll never know which value it’ll be returning beforehand): is there any way I can query for the last key inside "audio" ("2814462280" in this example)?
{
"test": {
"audio": {
"1175828715": {
"direction": "outbound",
},
"2814462280": {
"direction": "inbound",
}
}
}
}
2
Answers
Object.keys can be useful in this case. If you want to query for the last key. you can try following
if you want to get the last object you can add one line code
Hope this helps you.
I’ve wrote a function to dynamically construct jsonb path to get the last key value.
As was previously mentioned in the comments section Postgres can reorder the keys in a jsonb field.
Here is the demo.