1

I have json data stored something like this:

{"values":{"a":0.65, "b":0.35}} 

I want to return the key which has the largest value in "values"

So in above case, I want to return:

a

since it has the largest value

Some of the rows have no values in the data which will have:

{} 

as a default

I need to handle those entries with {} and return Null as well.

1 Answer 1

4

With sample data:

create temporary table tmp as
  select '{"values":{"a":0.65, "b":0.35}}'::json as j;
insert into tmp values ('{}'::json);

You could do something like:

select (
  select key from json_each_text(j::json->'values')
  order by value::float desc limit 1
) as k
from tmp;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.