Search code examples
postgresqljsonbtrim

Use Trim Function With jsonb_array_elements in PostgreS


SELECT BTRIM('"enterprise"', '"') 
> Output = enterprise

I need to use BTRIM function to trim the double quotes like above, with jsonb_array_elements function.

I have a query like below,

SELECT jsonb_array_elements(json_column->'Fields')->'field_name' as "column"
FROM table_a

Which returns

column
"Value1"
"Value2"
"Value3"

I need the output without double quotes like below.

column
Value1
Value2
Value3

Solution

  • Trim function is not required, just use ->> to get JSON array elements as text :

    SELECT jsonb_array_elements(json_column->'Fields')->>'field_name' as "column"
    FROM table_a