0

Suppose I have the following data

WITH test(id, data) AS (
  VALUES
    (1, '{"key1": "Some text"}'::jsonb),
    (2, '{"other_key": "Some longer text"}'::jsonb),
    (3, '{"key_3": "Short"}'::jsonb)
)
select ??? from test;

Note that the JSON data is simple key-value data. The key can be anything, the value is always a String.

I want to return the maximum number of characters of the value field. 16 in this case, select length('Some longer text');

1 Answer 1

3

You need to turn the values into a set, then you can operate on it:

WITH test(id, data) AS (
  VALUES
    (1, '{"key1": "Some text"}'::jsonb),
    (2, '{"other_key": "Some longer text"}'::jsonb),
    (3, '{"key_3": "Short"}'::jsonb)
)
select max(length(t.val))
from test, jsonb_each_text(data) as t(k,val);
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.