How to Fix Postgres Error 22034 More Than One SQL Json Item

Published
Length
4 min read

Luis Felipe Dias

All posts

The postgres error 22034 more than one sql json item happens when a scalar JSON query matches multiple values. You fix it by narrowing your path expression or using an array wrapper. Database engines enforce strict rules when they read JSON documents. A query that expects one value will fail if the path finds two. This rule protects your app from reading the wrong record by mistake. Teams see this error often when JSON payloads change shape over time. What causes postgres error 22034 more tha

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

The postgres error 22034 more than one sql json item happens when a scalar JSON query matches multiple values. You fix it by narrowing your path expression or using an array wrapper.

Database engines enforce strict rules when they read JSON documents. A query that expects one value will fail if the path finds two. This rule protects your app from reading the wrong record by mistake. Teams see this error often when JSON payloads change shape over time.

What causes postgres error 22034 more than one sql json item

Postgres maps this failure to SQLSTATE 22034 in its error spec. The engine uses the internal macro name ERRCODE_MORE_THAN_ONE_SQL_JSON_ITEM for this state. It falls under the data exception class of standard SQL errors.

You trigger it when a JSON path function expects a single scalar item. Instead, the path expression matches two or more elements in the document. For example, a wildcard search might hit several objects in a nested array. If the query asks for one string or number, Postgres stops the query.

Data shape drift is the most common root cause in production systems. An upstream service might start sending an array where it once sent one object. Another cause is a broad path filter that matches duplicate keys across nested levels. When the engine sees multiple items, it throws error 22034 right away.

This behavior keeps bad data out of your reports and state tables. Without this check, the database would have to guess which item to return. Guessing could put the wrong price, status, or user ID into your app.

How to diagnose and fix the failure in production

Start by finding the exact rows that hold multiple items at the target path. Run a read query that counts how many elements your path expression returns. Filter your table for rows where that item count is greater than one. This step shows you the exact documents that break your scalar query.

Keep permissions tight when engineers inspect these broken rows in production. In Postgres, you can grant the SELECT privilege on specific columns of a table. Granting read rights only on the JSON column and primary key limits data exposure. You can also set up group roles to manage privileges for many users.

JSON Web Tokens (JWT) + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Watch out for broad default permissions when you set up diagnostic roles. The special role name PUBLIC grants a privilege to every role on the system. By default, PUBLIC gets CONNECT and TEMPORARY on databases, plus EXECUTE on functions. In Postgres 15 and later, default settings stop all users from creating public objects. In older versions, admins must revoke CREATE on the public schema from PUBLIC.

Also remember that JSON documents often hold sensitive personal details. When engineers query raw payloads, data masking can rewrite matched fields in results. This step replaces emails, phone numbers, card numbers, and national IDs during the check.

Once you see the offending JSON structure, you have two main ways to fix it. First, you can make your path expression more specific. Add an array index or a tighter filter so the path hits only one item. This works best when you only care about the first match or a specific key.

Second, you can switch from a scalar extraction function to a query wrapper. Wrapping the output in a JSON array lets the query return multiple items safely. You can also use a set-returning function to turn each array item into a row. After you update the query, test it against the rows you found earlier.

How to prevent JSON path errors

You can stop error 22034 from coming back by checking data before it lands. Add check constraints on your table to validate the JSON structure on write. A constraint can verify that a target path always holds a single scalar value. If an app tries to write an array there, the database rejects the write early.

You should also test your path queries against varied sample data in staging. Include documents with empty arrays, single items, and multiple nested items. Clear rules on JSON schemas help backend teams avoid breaking downstream queries.

Check every Postgres query on the wire

Database roles and network rules decide who can open a session. They do not read what each SQL statement does after login. With hoop.dev, you run an open-source proxy, a sidecar for your Postgres, inline in the Postgres wire protocol. On every query an AI agent or engineer runs against production, hoop.dev decides to pass, mask, block, or hold it for human review. Your apps and agents keep their login details and change only the host and port in the connection string their tool uses.

Before a query runs, hoop.dev guardrails refuse bad deletes or drops with a real Postgres error, or hold risky queries for approval. On the way back, hoop.dev data masking rewrites sensitive values in returned rows in memory, and every verdict goes to the audit trail. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and generates evidence for HIPAA, GDPR, and PCI DSS. See the code on GitHub.

Frequently asked questions

What does SQLSTATE 22034 mean in Postgres?

SQLSTATE 22034 is a standard data exception named more_than_one_sql_json_item. It means a JSON path query expected one scalar value but found multiple items.

Can I fix error 22034 without changing the stored JSON data?

Yes, you can update your SQL query instead of rewriting the table data. Narrow the JSON path to pick one index, or wrap the result in an array.

How can engineers inspect broken JSON rows safely?

Admins can grant SELECT on specific columns to a read-only group role. Pairing column grants with field masking for emails and card numbers keeps checks safe.

The query runs. The emails stay hidden.