Recommended Free Tools
Two JSON objects written back to back are not one JSON document, so JSON.parse() rejects {"id":1}{"id":2}. The fix depends on what the two objects mean. If they belong to one collection, write them as a single array, [{"id":1},{"id":2}]. If they are separate records, parse each one on its own.
Why the input fails
RFC 8259, the IETF standard for JSON published in December 2017, defines a JSON text as optional whitespace, exactly one JSON value, and optional whitespace again. Its grammar is written as JSON-text = ws value ws. A single object is one value. Placing a second object directly after the first adds a second top-level value with nothing to join them, so the whole string is not one valid JSON text.
As an Amazon Associate I earn from qualifying purchases.
The parser handles this in order. It reads the first object to its closing brace, treats that as a complete value, and then meets a { that is not whitespace. MDN’s reference for JSON parse errors lists this condition as “unexpected non-whitespace character after JSON data.” Each engine words the message differently, and some report a position, so do not match on the exact text.
Whitespace does not change the outcome. A newline between the objects is still whitespace surrounding a second value, so the following input fails in the same way:
#1 Best Overall
// Fails: two top-level values, separated only by a newline
JSON.parse('{"id":1}
{"id":2}');
When to wrap the objects in an array
Wrap the objects in an array when they are elements of one collection and the consumer expects one parsed value that holds all of them:
const records = JSON.parse('[{"id":1},{"id":2}]');
// records is [{ id: 1 }, { id: 2 }]
The outer square brackets make one top-level array, and the comma separates its two elements. RFC 8259 describes arrays as zero or more comma-separated values inside square brackets.
Rank #2
If you build the text yourself, serialize the array
When your code creates the objects, avoid concatenating strings. Put the objects in an array and serialize it once:
const text = JSON.stringify([{ id: 1 }, { id: 2 }]);
// '[{"id":1},{"id":2}]'
const records = JSON.parse(text);
This produces valid output by construction, including correct escaping of any string values.
Do not patch existing text with a regular expression
A common shortcut is to replace every boundary between objects with a comma and wrap the result in brackets. This can silently change data. Consider this input, where a string value happens to contain }{:
const input = '{"note":"a}{b"}{"id":2}';
const patched = '[' + input.replace(/}s*{/g, '},{') + ']';
JSON.parse(patched);
// [{ note: 'a},{b' }, { id: 2 }] -- the note value changed from 'a}{b'
The pattern matched text inside a string literal as well as the real boundary. The result is valid JSON, so no error is raised. If you control the producer, fix the producer. If you must repair input you do not control, parse each object individually with a tokenizer that understands strings, or ask the source to fix its framing.
Rank #4
When the objects are separate records
If each object is an independent message or row, an array changes the meaning of the data. The consumer would receive one value where it expects several. In that case, parse each JSON text separately, using the framing the producer documents. RFC 8259 specifies what one JSON text is, but it does not define how a stream of several texts should be delimited, so that framing comes from the producer or from the protocol carrying the data.
const first = JSON.parse('{"id":1}');
const second = JSON.parse('{"id":2}');
When the producer writes one record per line, split on the line boundary and parse each line. Confirm that the producer really uses that convention, and handle a record that spans lines or contains embedded newlines in a string value.
Best Value
Choosing between the two representations
| Situation | Representation | Use it when |
|---|---|---|
| One collection of items | A single JSON array, such as [{"id":1},{"id":2}] |
The consumer expects one parsed value that contains every object. |
| Independent records or messages | Separate JSON texts, parsed one at a time under an explicit framing convention | Each object is its own record, and the consumer processes them one by one. |
| Text you cannot change, with unknown intent | Establish the intended structure before choosing either option | The source documentation or a sample from the producer shows which model it uses. |
A newline alone is not a delimiter for a JSON array, and it does not make adjacent objects one JSON text. Decide the data shape first, then pick the representation.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Troubleshooting when parsing still fails
- Check the full string. Log the exact value passed to
JSON.parse(), not a console rendering or only the first object. Hidden characters and truncation often cause the failure. - Count top-level values. If the input has two objects and the caller expects one collection, wrap it in brackets and add a comma between them. If the input contains more than two objects, check that the producer intended a collection before wrapping.
- Check the grammar inside each object. JSON strings must use double quotes, and arrays and objects cannot contain trailing commas. Single-quoted keys,
undefined, comments, and unquoted keys all cause failures that look similar to the adjacency error. - Remember that
JSON.parse()reads JSON, not JavaScript. An object literal copied from source code may be valid JavaScript but not valid JSON. Keys must be quoted strings, for example. - Separate the causes. If the error appears after wrapping, the problem is inside the objects, not in the adjacency. Parse each object alone to find which one is malformed.
MDN’s JSON.parse() reference documents that malformed input throws a SyntaxError. Catching that error lets your code report which record or source failed, rather than failing the entire batch without context.
try {
const records = JSON.parse(text);
} catch (err) {
if (err instanceof SyntaxError) {
console.error('Invalid JSON from source:', err.message);
}
}
The error message is engine-specific, so log it for diagnosis but do not branch your logic on its wording.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




