2

I have a JSONB column called metrics in a table events. It stores various metrics as a flat hash, e.g.

{"m1": 123, "m2": 122.3, "m3": 32}  

I would like to extract all the values stored in that column. Is it possible? I have found a function jsonb_object_keys(jsonb), but I failed to find anything similar for values.

2
  • 2
    There are a few json & jsonb functions available for that purpose. Which one to choose depends on your particular case. Commented Dec 4, 2015 at 16:35
  • Thanks, I would like to calculate min value of this json and select all records that has min above some threshold. I tried functions you linked but I failed to achieve what I want. Do you have any idea how to do that? Commented Dec 7, 2015 at 10:22

3 Answers 3

3

Use jsonb_each() for this purpose:

WITH json_test(data) AS ( VALUES
  ('{"m1": 123, "m2": 122.3, "m3": 32}'::JSONB)
)
SELECT element.value 
FROM json_test jt, jsonb_each(jt.data) as element;

Output:

 value 
-------
 123
 122.3
 32
(3 rows)
Sign up to request clarification or add additional context in comments.

Comments

2

Use jsonb_each() in a lateral join:

with val as (
    select '{"m1": 123, "m2": 122.3, "m3": 32}'::jsonb js
    )
select key, value
from val,
lateral jsonb_each(js);

 key | value 
-----+-------
 m1  | 123
 m2  | 122.3
 m3  | 32
(3 rows)    

Comments

0

Using json_each you can extract the values with:

SELECT value FROM json_each('{"m1": 123, "m2": 122.3, "m3": 32}')

Output

value
-----
123
122.3
32

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.