Invalid JSON Syntax Used for Array Input: Causes, Fixes, and Worked Examples
Fix 'invalid JSON syntax used for array input' errors fast—causes, worked examples, and edge cases explained.
“Invalid JSON syntax used for array input” means the JSON you supplied starts with [ (an array) but the receiving system or plugin expects a JSON object starting with {. The fix is either to wrap the array in an object, extract a single object from the array, or serialize the value correctly before passing it.
Why Array Input Triggers an Invalid JSON Syntax Error
JSON has exactly two top-level container types: objects ({}) and arrays ([]). Many tools, plugins, and database columns are configured to accept only one of those types. When you paste or pass a JSON array where an object is expected, the parser rejects it immediately with a message like “Invalid JSON syntax used for object input” or “invalid input syntax for type json.”
Beyond the object-vs-array mismatch, arrays can also be malformed in their own right. The most common internal array syntax errors are:
- Trailing commas — a comma after the last element (
[1, 2, 3,]) is not allowed in JSON even though JavaScript permits it. Parsers typically report this asUnexpected token ], pointing at the bracket rather than the comma. - Missing commas between elements —
["apple" "banana"]is invalid; every element must be separated by a comma. - Single quotes instead of double quotes —
['apple', 'banana']is invalid JSON; strings must use double quotes. - Unescaped backslashes or quotes inside strings — a Windows path like
"C:\Users\data"inside an array element is invalid because\Uis not a recognized escape sequence; it must be"C:\\Users\\data". - NaN, Infinity, or undefined as element values — these JavaScript literals are not valid JSON values; replace them with
null.
Complete Worked Example: Diagnosing and Fixing an Array Input Error
Suppose you have a shopping-cart component whose value is a JavaScript array of product objects, and you want to insert it into a PostgreSQL jsonb column via a parameterized query. You pass the value directly and receive:
invalid input syntax for type json: "{}"
Step 1 — Inspect what is actually being sent.
Run a diagnostic query:
SELECT '{{ cart.value }}' AS raw_value;
Result:
"{\"id\":1,\"name\":\"Espresso\"}"
The value is a string representation of an object, not a proper JSON array. The template substitution inserted the JavaScript .toString() of the array, which collapses to [object Object] or a partial string — not valid JSON.
Step 2 — Serialize the array correctly.
Wrap the interpolation with JSON.stringify:
SELECT {{ JSON.stringify(cart.value) }}::jsonb AS cart_data;
Now the value passed to the query is a proper JSON string:
[
{"id": 1, "name": "Espresso", "qty": 2, "price": 3.50},
{"id": 2, "name": "Croissant", "qty": 1, "price": 2.75}
]
The ::jsonb cast succeeds because the input is now a valid JSON array.
Step 3 — Verify the array’s own syntax before sending.
Before passing any array to a system, mentally check it against these rules (all of which make JSON stricter than JavaScript):
// INVALID — trailing comma after last element
["Espresso", "Latte",]
// INVALID — single quotes
['Espresso', 'Latte']
// INVALID — missing comma between elements
["Espresso" "Latte"]
// INVALID — unescaped backslash in a string element
["C:\Users\barista"]
// VALID
["Espresso", "Latte"]
// VALID — escaped backslash
["C:\\Users\\barista"]
Use the free JSON Formatter on this site to paste your array and immediately see the exact line and column of any syntax problem before you send it anywhere.
Step 4 — Handle the object-vs-array mismatch.
If the target system expects a single JSON object and you have an array, you have two options:
- Extract one element:
cart.value[0]gives{"id": 1, "name": "Espresso", "qty": 2, "price": 3.50}— a valid object. - Wrap the array in an object:
{ "items": [... ] }— the outer container is now an object, satisfying the parser.
How the Receiving System Changes the Fix
The correct fix depends on what is consuming the array:
| Consumer type | What it expects | Fix |
|---|---|---|
| Workflow plugin (e.g., RSS-type trigger) | A single JSON object | Extract one element from the array |
PostgreSQL jsonb column |
Valid JSON (object or array) | Serialize with JSON.stringify before interpolation |
REST API endpoint typed as object |
A JSON object {} |
Wrap array in {"data": [...]} |
REST API endpoint typed as array |
A JSON array [] |
Ensure serialization produces [...], not "[object Object]" |
The root cause in every case is the same: JSON built by string concatenation or direct template interpolation breaks the moment a value contains a quote, a backslash, a newline, or is itself a complex type. Using a proper serializer — JSON.stringify in JavaScript, json.dumps in Python — escapes all of those correctly every time.
Edge Cases Where the Simple Fix Fails
Double encoding. If you call JSON.stringify on a value that is already a JSON string, you get a string-of-a-string: "[{\\\"id\\\":1}]". The database or API receives a string, not an array. Check the query execution metadata to see the raw value being sent; if it is wrapped in extra quotes, remove one layer of serialization.
Truncated arrays. An error message of Unexpected end of JSON input on an array that looks complete usually means the array was cut off — by a timeout, a field-length cap, or a stream that was not read to completion. The JSON is not malformed; it is incomplete. Check the byte length against the Content-Length header or the field’s storage limit.
An HTML error page where the array should be. Unexpected token < at position 0 means the server returned an HTML page (a 404, a 500, or a login redirect) instead of a JSON array. There is nothing wrong with your array syntax; fix the HTTP request or handle the status code before parsing.
Invisible characters. Files saved by some editors start with a UTF-8 byte-order mark (BOM, U+FEFF). Some parsers reject it, producing an error on what appears to be a perfect first character. Word processors also replace straight double quotes with curly “smart quotes” that are not valid JSON string delimiters. A validator that shows the exact column catches both instantly.
NaN or Infinity inside array elements. JavaScript’s JSON.stringify converts NaN and Infinity to null automatically, but hand-edited JSON or output from other serializers may leave them as bare literals, which are invalid in JSON. Replace them with null or a sentinel string.
Quick Checklist Before Submitting Any JSON Array
- Does the array start with
[and end with]with no trailing comma after the last element? - Are all string values — including object keys inside array elements — wrapped in double quotes, not single quotes?
- Are backslashes and internal double quotes escaped (
\\and\")? - Is the value serialized with a proper serializer rather than string concatenation or template interpolation?
- Does the target system accept an array, or does it require a single object?
Answering “no” to any of these is the source of the error.