Common SQL Errors

PostgreSQL Error - 22037 non_unique_keys_in_a_json_object Error Explained

August 4, 2025

The error arises when PostgreSQL finds duplicate key names inside a JSON object during parsing, casting, or validation.

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is the non_unique_keys_in_a_json_object error?

non_unique_keys_in_a_json_object occurs when PostgreSQL finds duplicate key names in the same JSON object. Ensure each key appears only once or use jsonb_object_agg to deduplicate before inserting or casting. Removing duplicate keys clears the error quickly.

Error Highlights

Typical Error Message

non_unique_keys_in_a_json_object

Error Type

Data Validation Error

Language

PostgreSQL

Symbol

non_unique_keys_in_a_json_object

Error Code

22037

SQL State

Explanation

Table of Contents

What is the non_unique_keys_in_a_json_object error in PostgreSQL?

PostgreSQL raises condition 22037, labelled non_unique_keys_in_a_json_object, when it parses a JSON value that repeats a key name inside the same object. Every key in a JSON object must be unique so the database can form an unambiguous key-value map.

The error commonly appears while casting a text literal to json or jsonb, loading data from external sources, or validating input inside PL/pgSQL functions.

Ignoring it can break ETL jobs, API endpoints, and reporting queries that rely on valid JSON.

What Causes This Error?

Duplicate keys inside a JSON literal, parameter, or column trigger the failure at parse time.

PostgreSQL refuses to store or return malformed JSON, protecting downstream consumers from ambiguity.

ETL pipelines that merge records, poorly designed client code, or copy-paste mistakes often introduce the duplicated keys that surface as error 22037.

How to Fix non_unique_keys_in_a_json_object

First locate the offending JSON by running the original statement with RETURNING or by logging the payload.

Once found, remove or rename the repeated keys so that each name appears exactly once.

If you must deduplicate on the fly, cast to jsonb, unnest the key-value pairs, keep the first value per key, and rebuild the object with jsonb_object_agg.

Common Scenarios and Solutions

INSERT statements that load raw JSON files often fail. Pre-validate using jsonb_pretty or jsonb_path_exists to catch duplicates before insertion.

API layers that concatenate partial JSON strings can silently create repeated keys.

Use PostgreSQL's jsonb_build_object or framework serializers instead of string concatenation.

Best Practices to Avoid This Error

Always generate JSON with server-side functions like row_to_json or jsonb_build_object, which guarantee key uniqueness. Validate external JSON with jsonschema or PL/pgSQL checks during ingestion.

Integrate Galaxy's linting and AI copilot to flag duplicate keys while you type, preventing bad payloads from reaching production.

Related Errors and Solutions

22032 (invalid_json_text) fires when the JSON syntax itself is invalid. 22P02 (invalid_text_representation) appears when casting non-JSON text.

Their fixes focus on correcting syntax, not key uniqueness.

.

Common Causes

Related Errors

FAQs

Does PostgreSQL allow duplicate keys in jsonb?

No. PostgreSQL enforces unique keys for both json and jsonb types. jsonb additionally sorts keys on storage.

Can I keep the last duplicate value instead of the first?

Yes. Change the ORDER BY in the jsonb_object_agg query to ORDER BY key, max(ctid) to keep the last occurrence.

Which PostgreSQL versions raise this error?

All supported versions (9.4+) raise SQLSTATE 22037 when duplicate keys are encountered.

How does Galaxy help?

Galaxy's editor highlights JSON issues in real time and its AI copilot suggests corrected queries, preventing error 22037 before execution.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo