'postgresql, concat json values if not null

I'm using CONCAT with values from a jsonb columns. sometimes one of those values could be null (json).

how can i COALESCE the whole returned field xywh to NULL ?

query := `SELECT
              CONCAT( 
                data->'x',',',
                data->'y',',', 
                data->'w',',', 
                data->'h') as xywh 
              FROM table`


Solution 1:[1]

Because concat fucntion will ignore NULL

Concatenate all arguments. NULL arguments are ignored.

if you want to let the result be NULL if any JSON field is null, we can try to use || concatenate string.

select  data->>'x' || ',' ||
        data->>'y' || ',' ||
        data->>'w' || ',' ||
        data->>'h'  
FROM table`

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1