Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

JSON.parse() accepts one JSON text, and that text may contain only one top-level value. Two objects written next to each other, as in {"id":1}{"id":2}, are two top-level values, so the call throws a SyntaxError. If the two objects belong to one collection, the fix is to write them as a JSON array: [{"id":1},{"id":2}]. If they are independent records, wrapping them is the wrong fix, and each object should be parsed on its own under whatever framing the producer uses.

Why two objects side by side fail

Take the input below. The first object is a complete JSON value. The parser has finished reading it, and the next character is another {, which is non-whitespace data that follows a completed value.

JSON.parse('{"id":1}{"id":2}');
// SyntaxError

MDN’s JSON parse error reference lists “unexpected non-whitespace character after JSON data” as one of the common diagnostics for this situation. The exact message is engine-specific. Chrome, Firefox, and Node.js each word the error differently, and some versions report only the position of the problem. Match the category, not the wording, when you search for a fix.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What the JSON grammar allows at the top level

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. The formal rule is JSON-text = ws value ws. Whitespace can appear before and after that one value, but it does not separate one document from another. A line break between the two objects changes how the input looks, not how many top-level values it contains.

The same standard defines an array as zero or more values separated by commas and enclosed in square brackets. An array is itself one value, which is why wrapping the objects in brackets produces a valid JSON text.

When to wrap the objects in an array

Wrap the objects in an array when they are entries in one collection and the consumer expects a single parsed value that contains all of them. Add an opening [ before the first object, a comma between the objects, and a closing ] after the last one.

const records = JSON.parse('[{"id":1},{"id":2}]');
// records is [{ id: 1 }, { id: 2 }]

The outer brackets create one top-level array, and the comma separates its two elements. Each element is a complete object, so the result is an ordinary JavaScript array you can loop over.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When the strings already arrive separately

If your code already holds the two strings as separate values, parse each one. This needs no wrapper, and it keeps the records independent.

const first = JSON.parse('{"id":1}');
const second = JSON.parse('{"id":2}');

When the wrapper is the wrong fix

A wrapper only makes sense when the contents are valid JSON values and the intended result is one collection. Inserting commas or brackets into arbitrary input does not repair it. Two common cases call for a different approach:

  • Independent messages or log records. A producer that emits one object per message expects each message to be parsed separately. Wrapping the stream in an array changes the meaning of the data, and a consumer that reads one record at a time may break.
  • Truncated or corrupted output. If one object is cut off in transit, an array wrapper will still fail, and it can hide the real problem.

Choosing between an array and separate parses

Situation Representation Parsing approach
Both objects form one collection (for example, a list of records from one response) One JSON array, such as [{"id":1},{"id":2}] One JSON.parse() call that returns an array
Each object is an independent record or message Separate JSON texts, under the producer’s documented framing One JSON.parse() call per record
The input is malformed or truncated Not valid JSON in either form Fix the producer or the transport before choosing a wrapper

The deciding factor is the data contract, not the appearance of the string. A newline between objects does not make them a collection, and it does not make them separate messages either. Check what the producer documents and what the consumer expects.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshooting when parsing still fails

  1. Log the full string you pass to JSON.parse(). Do not rely on the first object or a console-formatted view, which can hide stray characters or missing commas.
  2. Confirm there are exactly the objects you expect. If you see a third object, a stray character, or a second copy of a header, the input has more top-level values than you assumed.
  3. If the caller expects one collection, wrap the objects in brackets and add the separating comma, then parse again.
  4. If the producer emits independent records, check its documentation for the framing it uses, and parse record by record. RFC 8259 specifies one JSON text and does not define a framing protocol for a stream of texts.
  5. If the wrapped version still fails, check the remaining grammar rules. JSON strings must use double quotes, and arrays and objects cannot contain trailing commas. A JavaScript object literal with single quotes or unquoted keys is not JSON, and JSON.parse() will reject it.
  6. If the input comes from a JavaScript source file rather than a string, remember that JSON.parse() reads JSON text only. It does not evaluate arbitrary JavaScript expressions.

In practice, most fixes come down to one decision: decide whether the two objects are one value or two. Once you know that, the parsing code follows directly.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.