Skip to main content
PHP Blog

Back to all posts

How to Merge Two Json_arrays In Postgresql?

Published on
4 min read
How to Merge Two Json_arrays In Postgresql? image

Best Tools to Merge JSON Arrays to Buy in February 2026

1 PostgreSQL: A Practical Guide for Developers and Data Professionals

PostgreSQL: A Practical Guide for Developers and Data Professionals

BUY & SAVE
Save 73%
PostgreSQL: A Practical Guide for Developers and Data Professionals
2 Beginning PHP and PostgreSQL 8: From Novice to Professional (Beginning: From Novice to Professional)

Beginning PHP and PostgreSQL 8: From Novice to Professional (Beginning: From Novice to Professional)

  • AFFORDABLE PRICES FOR QUALITY PRE-OWNED LITERATURE.
  • RIGOROUSLY CHECKED FOR READABILITY AND MINIMAL WEAR.
  • ECO-FRIENDLY CHOICE: REDUCE WASTE BY REUSING BOOKS!
BUY & SAVE
Save 29%
Beginning PHP and PostgreSQL 8: From Novice to Professional (Beginning: From Novice to Professional)
3 Full-Stack Web Development with TypeScript 5: Craft modern full-stack projects with Bun, PostgreSQL, Svelte, TypeScript, and OpenAI

Full-Stack Web Development with TypeScript 5: Craft modern full-stack projects with Bun, PostgreSQL, Svelte, TypeScript, and OpenAI

BUY & SAVE
Full-Stack Web Development with TypeScript 5: Craft modern full-stack projects with Bun, PostgreSQL, Svelte, TypeScript, and OpenAI
4 Procedural Programming with PostgreSQL PL/pgSQL: Design Complex Database-Centric Applications with PL/pgSQL

Procedural Programming with PostgreSQL PL/pgSQL: Design Complex Database-Centric Applications with PL/pgSQL

BUY & SAVE
Save 29%
Procedural Programming with PostgreSQL PL/pgSQL: Design Complex Database-Centric Applications with PL/pgSQL
5 PostgreSQL for Python Web Development with Flask: A Practical Guide to Building Database-Driven Web Applications

PostgreSQL for Python Web Development with Flask: A Practical Guide to Building Database-Driven Web Applications

BUY & SAVE
PostgreSQL for Python Web Development with Flask: A Practical Guide to Building Database-Driven Web Applications
6 DEUOTION T-post Clips Tool, Fixing Fence Clip and Wire Steel Bender T-post Handheld Twisting Tool, Multi Functional Bender

DEUOTION T-post Clips Tool, Fixing Fence Clip and Wire Steel Bender T-post Handheld Twisting Tool, Multi Functional Bender

  • RAPIDLY SECURE T-POST CLIPS, SAVING TIME ON INSTALLATIONS.
  • EASY-TO-USE DESIGN MAKES IT PERFECT FOR PROS AND DIYERS ALIKE.
  • DURABLE STEEL CONSTRUCTION ENSURES LONG-LASTING OUTDOOR PERFORMANCE.
BUY & SAVE
Save 12%
DEUOTION T-post Clips Tool, Fixing Fence Clip and Wire Steel Bender T-post Handheld Twisting Tool, Multi Functional Bender
7 SQL Hacks: Tips & Tools for Digging Into Your Data

SQL Hacks: Tips & Tools for Digging Into Your Data

  • AFFORDABLE PRICING FOR QUALITY USED BOOKS.
  • THOROUGHLY INSPECTED FOR READABILITY AND MINOR WEAR.
  • ECO-FRIENDLY CHOICE: SUPPORT RECYCLING BY BUYING USED!
BUY & SAVE
Save 28%
SQL Hacks: Tips & Tools for Digging Into Your Data
+
ONE MORE?

To merge two JSON arrays in PostgreSQL, you can use the jsonb_set function along with the jsonb_agg function. First, you need to convert the JSON arrays to jsonb type using jsonb_array_elements function. Then, use the jsonb_agg function to aggregate the elements into a single JSON array. Finally, use the jsonb_set function to merge the two JSON arrays into one.

How to merge two json arrays in PostgreSQL using the jsonb_concat function?

To merge two JSON arrays in PostgreSQL using the jsonb_concat function, you can follow these steps:

  1. Convert the JSON arrays into JSON objects with a key like 'array1' and 'array2'.
  2. Use the jsonb_concat function to merge the two JSON objects.
  3. Extract the merged JSON array from the resulting JSON object.

Here is an example to demonstrate this:

-- create two JSON arrays CREATE TABLE json_data ( id serial PRIMARY KEY, data1 jsonb, data2 jsonb );

INSERT INTO json_data (data1, data2) VALUES ('{"array1": [1, 2, 3]}', '{"array2": [4, 5, 6]}');

-- merge the arrays using jsonb_concat SELECT jsonb_object_agg(key, value) -> 'merged_array' FROM ( SELECT * FROM json_data JOIN LATERAL jsonb_each(data1) ON TRUE UNION ALL SELECT * FROM json_data JOIN LATERAL jsonb_each(data2) ON TRUE ) AS combined_data;

In this example, we first create a table json_data with two JSON columns data1 and data2. We insert JSON arrays into these columns using the INSERT statement.

We then use the jsonb_each function to extract the key-value pairs from the JSON arrays and combine them using UNION ALL. Finally, we use jsonb_object_agg to aggregate the key-value pairs into a single JSON object and extract the merged array from it.

What is the jsonb_set function in PostgreSQL used for?

The jsonb_set function in PostgreSQL is used to update a JSON object stored in a jsonb column in a table. It allows you to specify the path to the value you want to update and the new value to be set at that path. This function is useful for modifying specific parts of a JSON object without having to completely replace the entire object.

How to merge two json arrays in PostgreSQL using the json_set function?

To merge two JSON arrays in PostgreSQL using the json_set function, you can follow these steps:

  1. Use the jsonb_agg function to aggregate the two JSON arrays into a single JSON array.
  2. Use the json_set function to update the JSON object with the new merged JSON array.

Here is an example query that demonstrates how to merge two JSON arrays using the json_set function:

WITH data AS ( SELECT '{ "array1": [1, 2, 3], "array2": [4, 5, 6] }'::jsonb AS json_data ) SELECT json_set( json_data, '{merged_array}', ( SELECT jsonb_agg(value) FROM ( SELECT jsonb_array_elements(json_data->'array1') UNION ALL SELECT jsonb_array_elements(json_data->'array2') ) AS subquery(value) ) ) FROM data;

This query will merge the two JSON arrays specified in the "array1" and "array2" keys of the JSON object and store the merged array in a new key called "merged_array".

By using the json_set function along with jsonb_agg and jsonb_array_elements functions, you can easily merge two JSON arrays in PostgreSQL.

How to merge two json arrays in PostgreSQL using the json_to_record function?

To merge two JSON arrays in PostgreSQL using the json_to_record function, you can follow these steps:

  1. Assume that you have two JSON arrays called json_array1 and json_array2 that you want to merge.
  2. Use the json_to_record function with the json_array_elements function to unnest each JSON array into rows.

SELECT * FROM json_to_record ( (SELECT json_array_elements(json_array1)) ) AS x(key text, value text), json_to_record ( (SELECT json_array_elements(json_array2)) ) AS y(key text, value text);

  1. This query will output the merged JSON arrays as rows with two columns key and value.

You can further manipulate the result to merge the two arrays into a single JSON array format if needed.