PostgreSQL rejects malformed JSON when casting or storing invalid syntax. This error (SQLSTATE 22P02) occurs when JSON parsing fails due to structural issues, missing quotes, or invalid tokens.
PostgreSQL enforces strict JSON syntax validation on both json and jsonb data types. When you attempt to cast a string to JSON or insert data into a JSON column, PostgreSQL parses the input. If the JSON structure violates the specification—such as missing colons, unquoted keys, invalid boolean values (like "True" instead of "true"), or incomplete objects—PostgreSQL immediately rejects the input with an "invalid input syntax for type json" error. This validation happens at the database level, ensuring data integrity and preventing corrupted JSON from being stored.
Use an online JSON validator or your application's built-in JSON parser to check for syntax errors. Common issues include missing quotes around keys, unquoted string values, capitalized booleans, or trailing commas.
Example of invalid vs. valid:
-- Invalid
INSERT INTO users (data) VALUES ('{"name": John, "active": True}');
-- Valid
INSERT INTO users (data) VALUES ('{"name": "John", "active": true}');JSON strictly requires lowercase true, false, and null. PostgreSQL will reject capitalized variants.
-- Wrong
SELECT '{"is_valid": True, "value": Null}'::jsonb;
-- Correct
SELECT '{"is_valid": true, "value": null}'::jsonb;JSON requires double quotes for both keys and string values. Single quotes are not valid JSON.
-- Wrong
SELECT '{'name': 'John'}'::json;
-- Correct
SELECT '{"name": "John"}'::json;Check for missing colons between keys and values, missing commas between pairs, or unmatched braces/brackets.
-- Wrong (missing value)
SELECT '{"name":}'::json;
-- Wrong (missing colon)
SELECT '{"name" "John"}'::json;
-- Correct
SELECT '{"name": "John"}'::json;When JSON is inside a SQL string, escape double quotes and other special characters properly. Use \\ for backslashes and \\" for literal quotes within the JSON string.
-- Wrong (unescaped quote in JSON string)
SELECT '{"description": "John said "Hello""}'::json;
-- Correct
SELECT '{"description": "John said \\"Hello\\""}'::json;Use your application language's JSON parsing library to validate before sending to PostgreSQL. This catches errors early and provides better error messages.
// JavaScript example
try {
const data = JSON.parse(userInput);
// Now it's safe to insert into PostgreSQL
} catch (error) {
console.error("Invalid JSON:", error.message);
}The jsonb() or json() cast functions validate syntax. For checking without throwing errors, use extensions like pg_jsonschema or write a wrapper function that catches errors.
-- This will throw if JSON is invalid
SELECT '{"valid": true}'::jsonb;
-- Safer approach: validate with a function
CREATE OR REPLACE FUNCTION is_valid_json(p_json text)
RETURNS boolean AS $$
BEGIN
p_json::jsonb;
RETURN true;
EXCEPTION WHEN OTHERS THEN
RETURN false;
END;
$$ LANGUAGE plpgsql IMMUTABLE;
SELECT is_valid_json('{"name": "John"}'); -- true
SELECT is_valid_json('invalid json'); -- falsePostgreSQL uses SQLSTATE 22P02 (data exception — invalid text representation) for all JSON parsing errors. The exact error message format is "invalid input syntax for type json" followed by details about what PostgreSQL expected at the failure point. For bulk operations, consider validating data in your application before bulk insert to avoid partial failures. When migrating legacy data to jsonb columns, you may discover that old text data contains invalid JSON—use a function like is_valid_json() in a WHERE clause to identify problematic rows before attempting the conversion. The jsonb type is preferred over json in modern PostgreSQL for its superior performance and operator support, though both enforce the same syntax validation.
ERROR: syntax error at end of input
Syntax error at end of input in PostgreSQL
Bind message supplies N parameters but prepared statement requires M
Bind message supplies N parameters but prepared statement requires M in PostgreSQL
Multidimensional arrays must have sub-arrays with matching dimensions
Multidimensional arrays must have sub-arrays with matching dimensions
ERROR: value too long for type character varying
Value too long for type character varying
insufficient columns in unique constraint for partition key
How to fix "insufficient columns in unique constraint for partition key" in PostgreSQL