When you see id1id2 fail JSON.parse, the cause is almost always that the string is not valid JSON — typically because it is a bare identifier or a concatenation of two values without quotes or separators. You should wrap it in an array only when the data is meant to be a list of values, not when it is a malformed single value.
Formula
The calculator above computes pace and speed from a finish time and distance. The formulas are:
- Pace = finish time (minutes) ÷ distance (km), expressed in min/km.
- Speed = distance (km) ÷ (finish time (minutes) ÷ 60), expressed in km/h.
Variables: t = finish time in minutes, d = distance in kilometers. Pace is minutes per kilometer; speed is kilometers per hour. The default values are t = 50 min and d = 10 km.
Worked Example
Using the defaults: t = 50 minutes, d = 10 km.
- Pace = 50 ÷ 10 = 5.00 min/km.
- Speed = 10 ÷ (50 ÷ 60) = 10 ÷ 0.8333 = 12.00 km/h.
The calculator shows pace = 5.00 min/km and speed = 12.00 km/h for the default inputs.
Background: Why JSON.parse Fails on id1id2
JSON (JavaScript Object Notation) has strict syntax rules. A valid JSON value must be one of: an object, an array, a string, a number, a boolean, or null. A bare identifier like id1id2 is none of these. It is not quoted as a string, not wrapped in brackets or braces, and not a numeric literal. When you call JSON.parse("id1id2"), the parser sees a character sequence that does not match any valid JSON token, so it throws a SyntaxError.
This often happens when developers try to parse a value that was never serialized to JSON in the first place. For example, if you have two variables id1 and id2 and you concatenate them without a separator, you get id1id2. That string is not JSON; it is just a plain string. The error is not a bug in the JSON parser — it is a sign that the input is not JSON at all.
How JSON.parse Actually Works
The JSON.parse() method takes a string and attempts to interpret it as a complete JSON document. It does not try to guess what you meant; it follows the JSON grammar exactly. The grammar allows whitespace around values, but the value itself must be one of the six types listed above. If the entire string does not match, the parse fails immediately.
Common valid examples:
JSON.parse("42")→ number 42JSON.parse("\"id1id2\"")→ string “id1id2” (note the escaped quotes)JSON.parse("[\"id1\",\"id2\"]")→ array of two stringsJSON.parse("{\"id1\":1,\"id2\":2}")→ object
If you forget the quotes around a string, you get a syntax error. If you forget the comma between array elements, you also get a syntax error. The error message from the browser or Node.js usually points to the exact position where parsing stopped, which helps debug the issue.
When to Wrap in an Array
You should wrap a value in an array when the data you are trying to parse is actually a list of values, not a single scalar. For example, if your API returns ["id1","id2"] but you accidentally strip the brackets, you get "id1","id2" which is not valid JSON. In that case, the fix is to add the brackets back, not to wrap the whole thing in another array.
However, if you have a single value that is not valid JSON on its own, wrapping it in an array makes it valid: JSON.parse("[" + id1id2 + "]") would work only if id1id2 is itself a valid JSON value. If id1id2 is a bare identifier, wrapping it in brackets still fails because the identifier is not a valid JSON element. You would need to quote it first: JSON.parse("[\"id1id2\"]").
So the rule is: wrap in an array only when the logical structure is a list. If you have a single string, add quotes. If you have a number, leave it bare. If you have an object, use braces. The array wrapper is not a universal fix for malformed JSON; it only works when the underlying data is meant to be a collection.
Common Problems and Mistakes
- Forgetting quotes around strings — the most frequent cause of
JSON.parseerrors. Always escape double quotes inside a string literal. - Trailing commas — JSON does not allow a comma after the last element in an array or object. Remove trailing commas.
- Single quotes — JSON requires double quotes for strings. Single quotes are not valid.
- Concatenating values without separators — as with
id1id2, this produces a single token that is not a valid JSON value. - Parsing a non-string —
JSON.parseexpects a string. If you pass an object or array, it is coerced to a string, which often leads to unexpected results.
How to Choose the Right Fix
Before adding an array wrapper, inspect the data source. Ask: what was the original JSON structure? If the source is an API, check the documentation. If you are building the string yourself, use JSON.stringify() instead of manual concatenation. That guarantees valid JSON output.
If you receive a string that is not JSON but you know it is a single value, decide its type:
- If it is a string, wrap it in double quotes.
- If it is a number, ensure it is numeric and not containing letters.
- If it is a boolean, use
trueorfalse. - If it is null, use
null.
If the value is actually a list of items, wrap the entire content in square brackets and ensure each item is properly quoted and separated by commas. For example, id1,id2 becomes ["id1","id2"].
Alternatives to JSON.parse
If you are dealing with data that is not strict JSON but similar, consider alternatives:
- eval() — can parse JavaScript expressions, but is dangerous and discouraged due to security risks.
- new Function() — similar to eval, also unsafe.
- Custom parser — if the format is simple, write a small parser using
split()or regex. - JSON5 — a superset of JSON that allows comments, trailing commas, and single quotes. Not native, but available as a library.
For most cases, fixing the JSON string is better than switching parsers. The error is usually a sign that the data was not serialized correctly.
Frequently Asked Questions
Why does JSON.parse throw “Unexpected token i”?
The token i is the first character of id1id2. Since i is not a valid start of a JSON value (like {, [, ", or a digit), the parser stops immediately. The error message often includes the character and position, which helps locate the problem.
Can I wrap any string in an array to make it parse?
No. Wrapping in an array only works if the inner content is itself valid JSON. If the inner content is a bare identifier or a malformed expression, the array wrapper does not help. You must first make each element valid JSON.
What is the difference between JSON.parse and JSON.stringify?
JSON.stringify() converts a JavaScript object or array into a JSON string. JSON.parse() does the reverse. If you use stringify to create the string, you can always parse it back without errors.
Why do I get “Unexpected end of JSON input”?
This error occurs when the string is empty or incomplete. For example, JSON.parse("") throws this. It can also happen if you cut off the string accidentally. Check that the full JSON string is present.
Is it safe to use eval() instead of JSON.parse?
No. eval() executes any JavaScript code, which can lead to security vulnerabilities if the input is untrusted. Always use JSON.parse() for JSON data. If you need to parse a non-standard format, write a dedicated parser.
Related
Formula used
in t | Finish time | min | 50 | 0 in d | Distance | km | 10 | 0 | 100000 out pace | Average pace | min/km | t / d | 2 out speed | Average speed | km/h | d / (t / 60) | 2
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Motorola Phone Recovery: How to Recover Data
- Poco X3 GT Dimensity 1100 67W Fast Charging Launched?5.00 min/km
- Flash Converter: What It Is & How to Use It
- Choosing a Pipe Flow Meter for Swirling Flow
- Convert 2D Videos to Oculus Rift VR: How-To
- DRAM Light on Motherboard: Meaning & Fixes5.00 min/km