1

I've managed to combine jsonb arrays using ||, but I'm not able to remove duplicates and 'N/A' from the jsonb array.

Current code :

SELECT
jsonb_path_query_array(column1, '$.key') ||
jsonb_path_query_array(column2, '$.key') ||
jsonb_path_query_array(column3, '$.key')
FROM
table;

Current output : (jsonb)

['N/A', 'N/A', 'N/A']
['N/A', 'AGENT', 'N/A']
['N/A', 'AGENT', 'AGENT']
['SYSTEM', 'N/A', 'N/A']

Desired output

NULL
AGENT
AGENT
SYSTEM
0

1 Answer 1

1

You can use a cte with array_to_json:

with vals(a) as (
  select jsonb_path_query_array(column1, '$.key') || jsonb_path_query_array(column2, '$.key') || jsonb_path_query_array(column3, '$.key') from data
),
r_na(a) as (
    select (select array_to_json(array_agg(distinct v.value)) 
            from jsonb_array_elements(v1.a) v where v.value::text <> '"N/A"')  
    from vals v1
)
select case when a::text = 'null' then null else a -> 0 end r from r_na;

Output:

null
AGENT
AGENT
SYSTEM
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.