I have a table with a column that contains a JSON body that has arrays that I want to sort based on a attribute associated with that array.
I have tried selecting the array name and displaying the attribute which will display the entire array
The column name is my_column
and the JSON is formatted as follows -
{
"num": "123",
"Y/N": "Y",
"array1":[
{
"name": "Bob",
"sortNum": 123
},
{
"name": "Tim Horton",
"sortNum": 456
}
]
}
I want the output to be based on the highest value of sortNum
so the query should display the attributes for Tim Horton. The code I have played around with is below but get an error when trying to query based on sortNum.
SELECT my_column
FROM
my_table,
jsonb_array_elements(my_column->'array1') elem
WHERE elem->>'sortNum' = INT