Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

PostgreSQL Error 22035: No SQL/JSON Item Explained

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

PostgreSQL error 22035 (no_sql_json_item) means a SQL/JSON function such as JSON_VALUE, JSON_QUERY or JSON_EXISTS could not find the item you asked for at the specified path, and the error was raised because the query did not tell PostgreSQL what to do when the item is missing. The fix is to add an ON EMPTY clause that supplies a default or returns NULL instead of failing. For example, change JSON_VALUE(doc, '$.name') to JSON_VALUE(doc, '$.name' DEFAULT NULL ON EMPTY) and the statement will run even when the key is absent.

What You'll Need

  • Access to a PostgreSQL database running version 12 or later, since the SQL/JSON path functions were introduced in PostgreSQL 12. The exact function set and clause support vary by major version; check the documentation for your version.
  • A SQL client such as psql, pgAdmin or another tool that can run ad-hoc queries.
  • The name of the table and JSON/JSONB column involved, and the path expression that is failing.
  • Permission to run SELECT and, if you need to change data, UPDATE on the table.

Step-by-Step

  1. Read the full error message. Error 22035 usually comes with a message like no SQL/JSON item and a detail line showing the path and the document. Note the exact path expression, for example $.customer.email. That path is what PostgreSQL evaluated and could not resolve.
  2. Reproduce the failure in isolation. Run a small query against the same value, for example SELECT JSON_VALUE('{"a":1}'::json, '$.b');. This confirms the error is caused by a missing path item rather than by a syntax error elsewhere in the statement. Labels and function signatures can differ slightly by PostgreSQL version, so compare against the docs for your installed release.
  3. Check whether the item really is missing. Query the document directly, for example SELECT doc FROM my_table WHERE id = 123; and inspect the JSON. Look for the key at the exact path you used. Common causes are a missing key, a key at a different nesting level, a case mismatch, or an array where you expected an object.
  4. Decide what should happen when the item is absent. You have three practical choices: return NULL, return a default value, or raise a custom error. For most reporting queries, returning NULL is the right choice. For data-quality checks, you may want an error.
  5. Add an ON EMPTY clause. Rewrite the call so the empty case is handled explicitly. For JSON_VALUE use JSON_VALUE(doc, '$.customer.email' DEFAULT NULL ON EMPTY). For JSON_QUERY the same clause applies. JSON_EXISTS does not raise 22035 for a missing item in the same way, but you can still use ON ERROR to control behaviour when the path itself is invalid.
  6. Handle the error case too. Add ON ERROR alongside ON EMPTY when the path might be malformed or the value might not be a scalar. For example JSON_VALUE(doc, '$.customer.email' DEFAULT NULL ON EMPTY DEFAULT NULL ON ERROR). This prevents a different error class from replacing 22035.
  7. Test with both present and absent items. Run the corrected query against a row where the key exists and a row where it does not. Both should return without error. Confirm the returned values are what you expect, especially if you used a default string.
  8. Apply the fix across the query or application. Search your codebase for other calls to JSON_VALUE, JSON_QUERY and JSON_EXISTS on the same column. The same missing-item assumption is often repeated in several places. Update each one with the appropriate ON EMPTY clause.
  9. Consider a schema or data fix as well. If the missing item indicates bad data, you may also want to correct the source data or add a CHECK constraint. PostgreSQL does not enforce a JSON schema by default, so validation has to be done in the application or with a constraint you write yourself.

Common Mistakes

  • Assuming JSON_VALUE returns NULL by default. In PostgreSQL, JSON_VALUE raises 22035 when the item is missing unless you specify ON EMPTY. Many people expect a silent NULL and are surprised by the error.
  • Using the wrong path syntax. SQL/JSON path expressions use $.key for an object member and $[0] for an array element. Mixing them up produces a missing-item error even when the data is present.
  • Forgetting case sensitivity. JSON keys are case-sensitive. $.Email will not match a key named email.
  • Confusing json and jsonb behaviour. The SQL/JSON functions work on both types, but jsonb normalises key order and duplicate keys. If your path depends on duplicate keys or exact whitespace, test with the actual column type.
  • Adding ON EMPTY but not ON ERROR. A malformed path or a non-scalar value can still raise a different error. If you want the query to be fully defensive, handle both clauses.
  • Ignoring version differences. The SQL/JSON functions and clause syntax were introduced in PostgreSQL 12 and have been refined in later releases. A query that works on one major version may not parse on another.

Tips

  • Use DEFAULT NULL ON EMPTY as a safe default for read-only reporting queries where a missing key is not an error condition.
  • Use ERROR ON EMPTY deliberately when a missing key should stop a data pipeline, and catch the error in your application.
  • Test path expressions with JSON_EXISTS first. It returns a boolean and is a quick way to confirm whether a path resolves before you call JSON_VALUE.
  • Keep path expressions in one place in your application code so a fix does not have to be repeated in many queries.
  • When debugging, select the raw JSON document and the path result side by side. That makes the mismatch obvious.

Troubleshooting

If adding ON EMPTY does not clear the error, check the following in order. First, confirm the error code is still 22035 and not a different SQL/JSON error such as 22036 or 22037, which indicate a different problem with the path or the value. Second, run the path expression with JSON_EXISTS to see whether it resolves at all. Third, verify the column type: a text column containing JSON must be cast to json or jsonb before the SQL/JSON functions will accept it. Fourth, check for trailing whitespace or invisible characters in the path string. Fifth, if the query is inside a function or prepared statement, make sure the clause is part of the same function call and not appended after it. Finally, if you are on an older PostgreSQL major version, confirm that the function and clause you are using are supported there; the SQL/JSON feature set is not identical across versions.

Frequently Asked Questions

What does error 22035 mean exactly?

It is the SQLSTATE for no_sql_json_item. PostgreSQL raises it when a SQL/JSON function cannot find the requested item at the given path and no ON EMPTY behaviour was specified.

Does JSON_VALUE always raise 22035 for a missing key?

Yes, by default. You must add an ON EMPTY clause to change that behaviour to returning NULL, a default value, or a custom error.

Can I fix it without changing the query?

Only by ensuring the data always contains the item at that path. Since PostgreSQL does not enforce JSON structure automatically, the reliable fix is to handle the empty case in the query with ON EMPTY.

Is this error specific to PostgreSQL?

The SQLSTATE 22035 is part of the SQL standard for SQL/JSON, so other databases that implement the standard may use the same code. The exact function names and clause syntax can differ between products.

What is the difference between ON EMPTY and ON ERROR?

ON EMPTY handles the case where the path is valid but the item is absent. ON ERROR handles cases where the path is malformed or the value cannot be converted to the requested type. Use both for fully defensive queries.

Does JSON_QUERY raise 22035 as well?

Yes, JSON_QUERY can raise the same error when the requested item is missing and no ON EMPTY clause is provided. The fix is the same: add an explicit ON EMPTY clause.