0

I have a table in postgres having column web with jsonb type. It contains values like below. I want to remove from the array. Can you please help.

Jsonb column only has values like below 2 rows

["test.com","test.com","abc.com"]
["google.com","fb.com","google.com"]

Required output

["test.com","abc.com"]
["google.com","fb.com"]
1

1 Answer 1

1

There is nothing built-in, but it's easy to write a function:

create function unique_elements(p_input jsonb)
  returns jsonb
as
$$
  select jsonb_agg(distinct t.element)
  from jsonb_array_elements(p_input) as t(element);
$$
language sql
immutable;

Then use it like this:

select unique_elements(web)
from the_table;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. This works. can you let me know what is the use of as t(element) here?
@hero_star: it's a table alias

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.