JSON Errors / MySQL

ERROR 3140: Invalid JSON text

How to Fix: ERROR 3140: Invalid JSON text

MySQL rejected a value being stored into a JSON column or passed to a JSON function. The full message includes the parser reason and position: "Invalid JSON text: "Invalid value." at position 1 in value for column..." - position is 0-based from the start of the document.

The same validation runs for JSON functions: JSON_EXTRACT('not json', '$.a') raises 3141/3140-family errors even in a SELECT.

{"find": "error"}paste your JSON below

Common causes

1. Single quotes or unquoted keys inside the value

Same rule as everywhere: SQL quotes the literal, JSON inside needs double quotes.

Invalid
INSERT INTO t (doc) VALUES ('{name: "Ada"}');
Valid
INSERT INTO t (doc) VALUES ('{"name": "Ada"}');

2. Application sent a PHP/Python structure string

print_r/var_export output or str(dict) is not JSON. Use json_encode (PHP) / json.dumps (Python) before binding the parameter.

3. Truncated document from a length-limited source

A VARCHAR staging column or log pipeline cut the JSON mid-string; the position in the error is near the end of the value.

How to fix it

Find bad rows before migrating a column to JSON
-- returns 0 for invalid documents so you can find them first
SELECT id, doc_text
FROM staging
WHERE JSON_VALID(doc_text) = 0
LIMIT 20;
Insert from PHP correctly
$payload = ["type" => "login", "userId" => 42];
$stmt = $pdo->prepare("INSERT INTO events (payload) VALUES (?)");
$stmt->execute([json_encode($payload, JSON_THROW_ON_ERROR)]);

Frequently asked questions

How do I interpret the position number in the error?

It is the 0-based character offset inside the JSON value where parsing failed - position 0 or 1 means the document is wrong from the start (quoting/encoding), a large position means a problem near the end (usually truncation or a stray character).

JSON column or TEXT column with JSON_VALID checks?

Use the JSON type: it validates on write (this error is that validation working), stores an optimized binary format, and enables JSON functions and multi-valued indexes. A TEXT column with a CHECK (JSON_VALID(col)) constraint is the fallback for exotic needs like preserving exact formatting.

Does MySQL allow duplicate keys in JSON?

It accepts the document and keeps the FIRST occurrence of a duplicate key - the opposite of most languages, which keep the last. A payload that behaves one way in your app can behave differently after a round-trip through MySQL; deduplicate keys at the producer.

Last updated:

Related JSON errors

You might also need

Free and in-browser, like everything on JSON Console. Browse all tools →