How to fix Invalid JSON text in PostgreSQL
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.
Quick fix
Fixes Invalid JSON text
-- Invalid
INSERT INTO users (data) VALUES ('{"name": John, "active": True}');
-- Valid
INSERT INTO users (data) VALUES ('{"name": "John", "active": true}');- Validate the JSON syntax before insertion
- Check for capitalized boolean and null values
- Use double quotes for all JSON strings
- Ensure all required syntax elements are present
-- InvalidINSERT INTO users (data) VALUES ('{"name": John, "active": True}');-- ValidINSERT INTO users (data) VALUES ('{"name": "John", "active": true}');Invalid JSON text
What this error means
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.
Typical symptoms
- Queries fail with "ERROR: invalid input syntax for type json" and SQLSTATE 22P02
- INSERT or UPDATE statements are rejected when storing JSON data
- Casting a text column to JSON fails: `column_name::json` throws an error
- Application receives database exceptions when trying to save JSON documents
- Validation fails mid-migration or during bulk data import
Common causes
- Malformed JSON structure with missing commas, colons, or quotes (e.g., `{"key":}` or `{key: "value"}`)
- Boolean or null values are capitalized (e.g., `True`, `False`, `Null` instead of lowercase)
- Unquoted object keys or string values (JSON requires double quotes, not single)
- Incomplete JSON like trailing commas, extra braces, or unexpected closing characters (e.g., `{"a":1,}` or `}}`)
- Special characters or line breaks in strings are not properly escaped
- Empty strings or completely empty input being cast as JSON (e.g., `""::json` or `NULL` cast as json)
- Using single quotes instead of double quotes in JSON strings
- Attempting to parse a non-JSON value like a plain number or boolean without wrapping it in valid JSON structure
How to fix "Invalid JSON text"
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'); -- falseAdvanced notes
PostgreSQL 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.
Frequently asked questions
- What does "Invalid JSON text" mean?
- 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.
- How do I fix "Invalid JSON text"?
- 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.
- What causes "Invalid JSON text"?
- Malformed JSON structure with missing commas, colons, or quotes (e.g., {"key":} or {key: "value"}) Boolean or null values are capitalized (e.g., True, False, Null instead of lowercase) Unquoted object keys or string values (JSON requires double quotes, not single)
- Is the "Invalid JSON text" error serious?
- It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- 📚PostgreSQL Official Documentation
- 📖PostgreSQL JSON Functions and Operators(Official Docs)
- 📖PostgreSQL Error Codes(Official Docs)