How to fix Duplicate JSON object key value in PostgreSQL

PostgreSQLINTERMEDIATEMEDIUMUpdated Dec 26, 2025ยท 110 views

PostgreSQL raises this error when JSON object constructors with UNIQUE constraints detect duplicate keys. Most often occurs with json() constructor, JSON aggregate functions, or when merging JSON objects.

Quick fix

Fixes 22030

SELECT json('{"a": 12, "a": 23}' WITH UNIQUE KEYS);
-- ERROR: duplicate JSON object key value
  1. Verify the JSON source contains duplicates
  2. Use WITHOUT UNIQUE KEYS if duplicates are acceptable
  3. Deduplicate keys before aggregation
  4. Handle duplicates at application level
postgresql โ€” error
SELECT json('{"a": 12, "a": 23}' WITH UNIQUE KEYS);
-- ERROR: duplicate JSON object key value
Duplicate JSON object key value

What this error means

JSON objects are key-value pairs where each key must be unique. In PostgreSQL 15+, the json() constructor with WITH UNIQUE KEYS option enforces this constraint. When you attempt to create or manipulate JSON data containing duplicate keys, PostgreSQL (error code 22030) signals that the JSON structure is invalid. For jsonb types, PostgreSQL silently keeps the last value; for json type with UNIQUE validation, it raises an error. This is a data integrity safeguard to prevent ambiguous JSON structures.

Typical symptoms

  • Error occurs when using json() constructor with WITH UNIQUE KEYS clause
  • json_object() or json_object_agg_unique() functions fail on duplicate keys
  • ERROR: duplicate JSON object key value appears during INSERT or UPDATE
  • JSON aggregate functions fail when processing grouped data with overlapping keys

Common causes

  1. Using json() constructor with WITH UNIQUE KEYS option on JSON containing duplicate keys
  2. json_object() or json_object_agg_unique() functions receiving duplicate key-value pairs
  3. Building JSON objects with duplicate keys from subqueries or dynamic data
  4. Merging JSON objects that happen to share the same keys without deduplication

How to fix "Duplicate JSON object key value"

1Verify the JSON source contains duplicates

Run a test query to confirm duplicate keys exist in your data:

sql
SELECT json('{"a": 12, "a": 23}' WITH UNIQUE KEYS);
-- ERROR: duplicate JSON object key value

Without the WITH UNIQUE KEYS clause, PostgreSQL accepts the duplicate (last value wins for jsonb):

2Use WITHOUT UNIQUE KEYS if duplicates are acceptable

If duplicate keys should be silently handled, switch to WITHOUT UNIQUE KEYS or use jsonb which keeps the last value:

sql
SELECT json('{"a": 12, "a": 23}' WITHOUT UNIQUE KEYS);
-- Result: {"a": 23}

SELECT jsonb('{"a": 12, "a": 23}');
-- Result: {"a": 23}
3Deduplicate keys before aggregation

If building JSON from SQL queries, ensure keys are unique before aggregating:

sql
SELECT json_object_agg_unique(key, value)
FROM (
  SELECT DISTINCT ON (key) key, value
  FROM my_table
  ORDER BY key, created_at DESC  -- Keep last value
) AS deduped;
4Handle duplicates at application level

If receiving JSON from external sources, deduplicate in your application before passing to PostgreSQL:

typescript
// JavaScript/TypeScript example
const deduped = Object.fromEntries(
  Object.entries(jsonData).reverse()
);

This keeps the first occurrence (due to reverse).

5Use jsonb_set for safe updates without duplicates

When building JSON incrementally, use jsonb_set which prevents duplicates:

sql
SELECT jsonb_set('{}', '{a}', '12')
       || jsonb_set('{}', '{a}', '23');
-- Result: {"a": 23} (last wins)

Advanced notes

PostgreSQL 16+ introduced a bug in the uniqueness check for JSON object constructors (fixed in 16.1), which could cause both false negatives (producing JSON with duplicate keys) and false positives (rejecting valid JSON). If you encounter inconsistent behavior with duplicate detection, ensure you are running PostgreSQL 16.1 or later. The json type (text-based) and jsonb type (binary) behave differently: jsonb only stores the last value for duplicate keys without raising an error, while the json() constructor with WITH UNIQUE KEYS enforces uniqueness at parse time. For most applications, jsonb is recommended for performance and automatic deduplication benefits.

Frequently asked questions

What does "22030" mean?
JSON objects are key-value pairs where each key must be unique. In PostgreSQL 15+, the json() constructor with WITH UNIQUE KEYS option enforces this constraint. When you attempt to create or manipulate JSON data containing duplicate keys, PostgreSQL (error code 22030) signals that the JSON structure is invalid.
How do I fix "22030"?
PostgreSQL raises this error when JSON object constructors with UNIQUE constraints detect duplicate keys. Most often occurs with json() constructor, JSON aggregate functions, or when merging JSON objects.
What causes "22030"?
Using json() constructor with WITH UNIQUE KEYS option on JSON containing duplicate keys json_object() or json_object_agg_unique() functions receiving duplicate key-value pairs Building JSON objects with duplicate keys from subqueries or dynamic data
Is the "22030" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading