Merging Concatenating JSON(B) columns in query

后端 未结 10 1564
不思量自难忘°
不思量自难忘° 2020-12-08 09:40

Using Postgres 9.4, I am looking for a way to merge two (or more) json or jsonb columns in a query. Consider the following table as an example:

10条回答
  •  忘掉有多难
    2020-12-08 09:59

    Also you can tranform json into text, concatenate, replace and convert back to json. Using the same data from Clément you can do:

    SELECT replace(
        (json1::text || json2::text), 
        '}{', 
        ', ')::json 
    FROM test
    

    You could also concatenate all json1 into single json with:

    SELECT regexp_replace(
        array_agg((json1))::text,
        '}"(,)"{|\\| |^{"|"}$', 
        '\1', 
        'g'
    )::json
    FROM test
    

    This is a very old solution, since 9.4 you should use json_object_agg and simple || concatenate operator. Keeping here just for reference.

提交回复
热议问题