I have the sample data for demostration as shown below:
Table:
create table tbl_jdata
(
id int,
jdata json
);
insert into tbl_jdata values(1,'[{"salary":10000,"name":"mak"},{"salary":20000,"name":"jak"},{"salary":45000,"name":"abc"}]');
I want to display only a json element which is having highest salary for example as shown below in expected result.
Expected Result:
id jdata
-------------------------------------
1 [{"salary":45000,"name":"abc"}]
My try:
select t.id,each_section
from tbl_jdata t
cross join unnest(t.jdata) each_section
where t.id = 1 and (each_section ->> 'salary') in
(
select max(each_section ->> 'salary')
from tbl_jdata t
cross join unnest(t.jdata) each_section
);
Getting an error:
ERROR: function unnest(json) does not exist
jsonforjdatacolumn and data has been stored in the given format.