Introduction
You paste your JSON into an application, hit run, and instead of the result you expected, you're staring at a cryptic error like "Unexpected token" or "Unexpected end of JSON input." It's one of the most common frustrations for anyone working with JSON, whether you're a beginner writing your first config file or an experienced developer debugging an API response at 2 a.m.
The good news is that JSON errors, while frustrating, are almost always caused by a small handful of predictable mistakes. Once you know what to look for, most JSON problems take just seconds to spot and fix.
In this guide, you'll learn the most common JSON errors, exactly what causes each one, and how to fix them — with clear before-and-after examples throughout. By the end, you'll be able to look at broken JSON and know almost instantly what's wrong.
What Are JSON Errors?
JSON errors occur when a piece of JSON text doesn't follow the strict syntax rules required for it to be considered valid. Unlike some more forgiving formats, JSON parsers are unforgiving by design — a single missing comma, an extra character, or an incorrect quote type is enough to cause the entire file or response to fail parsing.
These errors typically show up as messages like:
- •
Unexpected token in JSON at position X
- •
Unexpected end of JSON input
- •
SyntaxError: Expected property name
While the exact wording varies depending on the programming language or tool involved, these messages almost always point back to one of a small number of common structural issues.
Why It Matters
A single JSON syntax error can break far more than just a test script — it can bring down an entire application feature, corrupt a configuration file, or cause an API integration to silently fail.
Applications Depend on Valid JSON
Many applications read configuration settings, API responses, or stored data directly from JSON. If that JSON is malformed, the application may crash, fail to load, or behave unpredictably.
APIs Reject Malformed Requests
When sending data to an API, invalid JSON in the request body will typically cause the API to reject the entire request, even if only one small part of the JSON is broken.
Debugging Time Adds Up
Without knowing what to look for, tracking down a single misplaced comma in a large JSON file can take far longer than it should, especially in deeply nested structures.
Practical Example
Imagine a developer building a form that submits user data to a server as JSON. If a trailing comma accidentally makes its way into the submitted JSON, the entire request could fail, and depending on how the error is handled, the user might see a vague "Something went wrong" message with no indication of what actually broke.
Key Concepts
Before diagnosing specific errors, it helps to remember JSON's core syntax rules, since nearly every common error traces back to one of these.
JSON Requires Double Quotes
Both object keys and string values must be wrapped in double quotes (
"), never single quotes (
').
Commas Separate, But Don't Trail
Commas are required between items in an object or array, but must never appear after the final item.
Every Bracket Needs a Match
Every opening
{ or
[ must have a corresponding closing
} or
].
No Comments Allowed
Standard JSON does not support comments of any kind, even though many programming languages allow them in similar-looking syntax.
Keys Must Be Strings
In a JSON object, keys must always be strings wrapped in double quotes — never numbers, unquoted words, or other data types.
How It Works
When a program reads a JSON file or response, it uses a
parser to convert the JSON text into a usable data structure, like an object or array within that programming language. The parser reads through the text character by character, expecting a very specific, predictable pattern.
The moment the parser encounters something that doesn't match JSON's expected syntax — an extra comma, a missing bracket, an unquoted key — it stops and throws an error, often reporting the approximate position in the text where the problem was detected.
This is why JSON error messages often include a position or line number: the parser is telling you exactly where it got confused, even if the actual mistake is sometimes just before that reported location.
Step-by-Step Guide: How to Diagnose and Fix JSON Errors
Step 1: Read the Error Message Carefully
Most JSON error messages include a position, line number, or a snippet of the problematic text. Start there, even if it doesn't immediately make sense.
Step 2: Run the JSON Through a Formatter or Validator
Paste your JSON into a JSON formatter tool. Beyond just organizing your JSON visually, most formatters will flag the exact location of a syntax error, often more clearly than the original error message.
Step 3: Check the Area Immediately Before the Reported Error
JSON parsers often report the position where they realized something was wrong, which is frequently just after the actual mistake. If the error points to a certain character, check the few characters immediately before it.
Step 4: Look for the Most Common Culprits First
Before assuming something complex is wrong, check for the most frequent causes: missing commas, trailing commas, mismatched brackets, or incorrect quote types.
Step 5: Fix One Issue at a Time
If your JSON has multiple problems, fix the first error, re-validate, and repeat. Trying to fix everything at once in a large file can introduce new mistakes.
Step 6: Re-Validate After Every Fix
After each correction, re-run your JSON through a formatter or validator to confirm the fix worked before moving on.
Step 7: Format the Final Version for Readability
Once your JSON is valid, use a formatter to apply consistent indentation, making the file easier to read and maintain going forward.
The Most Common JSON Errors (and How to Fix Them)
1. Trailing Comma
The Problem:
``
json
{
"name": "Daniel",
"age": 28,
}
`
That comma after 28
is invalid, since it's the last item in the object.
The Fix:
`
json
{
"name": "Daniel",
"age": 28
}
`
Always double-check the last item in every object or array to make sure there's no trailing comma.
2. Missing Comma Between Items
The Problem:
`
json
{
"name": "Daniel"
"age": 28
}
`
Without a comma between "name": "Daniel"
and "age": 28
, the parser doesn't know where one property ends and the next begins.
The Fix:
`
json
{
"name": "Daniel",
"age": 28
}
`
3. Using Single Quotes Instead of Double Quotes
The Problem:
`
json
{ 'name': 'Daniel' }
`
JSON strictly requires double quotes for both keys and string values.
The Fix:
`
json
{ "name": "Daniel" }
`
4. Unquoted Keys
The Problem:
`
json
{ name: "Daniel" }
`
This kind of syntax is valid in JavaScript objects but not in standard JSON, where every key must be a quoted string.
The Fix:
`
json
{ "name": "Daniel" }
`
5. Mismatched or Missing Brackets
The Problem:
`
json
{
"name": "Daniel",
"age": 28
`
The closing }
is missing entirely.
The Fix:
`
json
{
"name": "Daniel",
"age": 28
}
`
A good habit is to count your opening and closing brackets whenever a "Unexpected end of JSON input" error appears, since this error often indicates something wasn't properly closed.
6. Using Comments
The Problem:
`
json
{
"name": "Daniel" // this is a comment
}
`
JSON does not support comments in any form.
The Fix:
`
json
{
"name": "Daniel"
}
`
If documentation is needed, keep it in a separate file rather than inside the JSON itself.
7. Improperly Escaped Characters
The Problem:
`
json
{ "quote": "She said "hello" to everyone." }
`
The unescaped quotation marks inside the string break the structure, since the parser interprets them as the end of the string value.
The Fix:
`
json
{ "quote": "She said \"hello\" to everyone." }
`
Any double quote that appears inside a string value must be escaped with a backslash.
8. Using Undefined or Non-JSON Values
The Problem:
`
json
{ "value": undefined }
`
undefined
is a valid concept in JavaScript, but it is not a valid JSON value.
The Fix:
Use null
instead, if the intent is to represent an empty or missing value:
`
json
{ "value": null }
`
9. Numbers with Leading Zeros or Invalid Formatting
The Problem:
`
json
{ "code": 007 }
`
JSON numbers cannot have leading zeros (except for the number zero itself).
The Fix:
Wrap it as a string if the leading zero is meaningful, such as for a code or ID:
`
json
{ "code": "007" }
`
10. Extra or Missing Data Outside the Root Structure
The Problem:
`
json
{
"name": "Daniel"
}
{
"extra": "data"
}
`
A JSON document can only have a single root value — either one object or one array — not multiple separate structures side by side.
The Fix:
Combine them into a single valid structure, such as an array of objects:
`
json
[
{ "name": "Daniel" },
{ "extra": "data" }
]
`
Real-World Examples
For Developers Debugging an API Response
A developer receiving a "SyntaxError: Unexpected token" error while parsing an API response might discover the API accidentally returned an HTML error page instead of JSON, which is a common cause of this specific error when working with third-party APIs.
For Students Submitting a Coding Assignment
A student writing their first configuration file might repeatedly forget commas between properties, a mistake nearly every beginner makes early on, easily caught by running the file through a validator before submission.
For Content Managers Editing Structured Data
A content manager manually editing a JSON-LD schema markup block on a webpage might accidentally leave a trailing comma while adding a new field, breaking the structured data without any obvious visual sign on the live page.
For Marketers Configuring a Tool's Settings File
A marketer updating a JSON-based configuration file for a marketing automation tool might use single quotes out of habit from writing plain text, unknowingly breaking the file's validity.
Benefits of Knowing How to Fix JSON Errors
- •Faster debugging, since you'll recognize common patterns immediately instead of starting from scratch each time
- •Fewer application crashes caused by malformed configuration or data files
- •More reliable API integrations, since properly validated JSON reduces failed requests
- •Improved confidence editing JSON manually, without needing to rely entirely on external tools
- •Better collaboration, since consistently valid, well-formatted JSON is easier for teammates to read and work with
Common Mistakes (Beyond Syntax)
Editing JSON Without Validating Afterward
Making manual edits to a JSON file and assuming it's still valid, without re-checking, is one of the most common ways small errors slip through unnoticed.
Copy-Pasting from Non-JSON Sources
Copying object-like syntax from a JavaScript file or another language's data structure often introduces subtle incompatibilities, like unquoted keys or single quotes, that aren't valid in standard JSON.
Ignoring the Reported Error Position
Skipping past the position information in an error message, rather than using it as a starting point for investigation, often leads to longer, more frustrating debugging sessions.
Assuming the Entire File Is Broken
A single small error can trigger a parsing failure for an entire file, even if 99% of the JSON is perfectly valid. Don't assume a large-scale rewrite is needed — usually, only one small fix is required.
Best Practices
- •Validate JSON immediately after editing it manually, rather than waiting until it causes a downstream error.
- •Use a JSON formatter during development to catch errors early and keep your files consistently readable.
- •Avoid copy-pasting JavaScript object syntax directly as JSON, since the two look similar but follow different rules.
- •Escape special characters properly whenever strings contain quotes, backslashes, or other reserved characters.
- •Keep JSON structures as flat as reasonably possible, since deeply nested JSON is more error-prone to edit manually.
- •Use version control for important JSON configuration files, making it easy to review exactly what changed if something breaks.
- •Build the habit of validating before saving, especially for configuration files that other systems depend on.
Comparison
Common JSON Errors at a Glance
| Error Type | Typical Cause | Fix |
| Trailing comma | Extra comma after the last item | Remove the comma |
| Missing comma | No comma between properties | Add a comma between items |
| Single quotes | Using ' instead of " | Replace with double quotes |
| Unquoted keys | Keys written without quotes | Wrap keys in double quotes |
| Mismatched brackets | Missing {, }, [, or ] | Add the missing bracket |
| Comments included | // or / / used in JSON | Remove comments entirely |
| Unescaped quotes | Quotes inside a string not escaped | Add backslash before internal quotes |
Using undefined | Non-JSON value used | Replace with null` |
| Leading zeros in numbers | Invalid number formatting | Convert to a string if needed |
| Multiple root values | More than one top-level structure | Wrap values in a single array or object |
Manual Debugging vs Using a JSON Validator
| Approach | Manual Debugging | JSON Validator/Formatter Tool |
| Speed | Slower, especially for large files | Fast, often instant |
| Accuracy | Prone to missing small errors | Reliably flags exact error location |
| Skill required | Requires familiarity with JSON syntax | Minimal, beginner-friendly |
| Best for | Small, simple JSON snippets | Any size or complexity of JSON |
Frequently Asked Questions
1. Why does my JSON say "Unexpected token" at a specific position?
This means the parser encountered something it didn't expect at that location, often caused by a missing comma, an extra comma, or an incorrect quote type nearby.
2. Why does my JSON say "Unexpected end of JSON input"?
This error typically means a closing bracket or brace is missing, causing the parser to reach the end of the file before the structure was properly closed.
3. Can JSON have comments?
No. Standard JSON does not support comments in any form. If you need to document a JSON file, keep notes in a separate file instead.
4. Why do I get an error even though my JSON looks correct?
Errors are often caused by subtle issues that are easy to miss visually, like a trailing comma, single quotes instead of double quotes, or an unescaped character inside a string.
5. Is it okay to use single quotes in JSON?
No. JSON requires double quotes for both keys and string values. Single quotes will cause a parsing error.
6. What's the easiest way to find a JSON error?
Paste your JSON into a JSON formatter or validator tool, which will typically highlight the exact location and nature of the error more clearly than a raw error message.
7. Can I have multiple JSON objects in one file?
Not as separate root-level structures. If you need multiple objects, wrap them inside a single array.
8. Why does my JSON error mention a completely different line than where my mistake is?
Parsers report the position where they realized something was wrong, which is often just after the actual mistake. Check the area immediately before the reported location.
9. Does JSON support trailing commas like some programming languages do?
No. Trailing commas are not allowed in standard JSON, even though some languages and formats (like JavaScript object literals) do permit them.
10. Can numbers in JSON have leading zeros?
No, except for the number zero itself. If a leading zero needs to be preserved (like in a code or ID), wrap the value in quotes as a string instead.
11. Why does copying code from JavaScript into JSON cause errors?
JavaScript object syntax is more flexible than JSON — it allows unquoted keys, single quotes, and trailing commas, none of which are valid in strict JSON.
12. How can I prevent JSON errors before they happen?
Validate your JSON immediately after editing it, use a formatter tool during development, and build the habit of double-checking commas and brackets whenever you add or remove a property.
Troubleshooting
Problem: My API is returning valid-looking JSON, but my code still throws a parsing error.
Check whether the API might be returning an error page (often HTML) instead of JSON under certain conditions, such as an expired API key or an invalid request.
Problem: My JSON file was working fine until I added one new field.
Review the exact area around your most recent edit first — a missing or extra comma near a newly added field is one of the most common causes of a sudden, previously-working file breaking.
Problem: My JSON validator flags an error, but I can't find anything wrong nearby.
Check slightly earlier in the file than the reported position, since parsers often report errors slightly after the actual mistake occurred.
Problem: My deeply nested JSON is too complex to debug manually.
Use a formatter tool to auto-indent the structure, making it much easier to visually trace which bracket or brace belongs to which section.
Problem: My JSON keeps breaking every time I edit it by hand.
Consider editing JSON within a code editor that offers real-time syntax highlighting and error detection, which can catch many mistakes before you even attempt to run or save the file.
Related Tools
If you work with JSON regularly, these free tools can save significant time and frustration:
- •JSON Formatter — instantly validate your JSON, catch syntax errors, and neatly organize the structure for easier reading.
- •Base64 Encoder — useful when JSON values need to include encoded binary data.
- •URL Encoder — handy when JSON values contain URLs that require safe encoding.
- •Word Counter — helpful when documenting JSON structures in accompanying API documentation.
These tools are all free to use directly on webgigo.com, with no downloads or sign-ups required.
Conclusion
JSON errors can feel intimidating at first, especially when the error message doesn't clearly point to the actual problem. But as you've seen throughout this guide, the vast majority of JSON issues come down to a small, predictable set of mistakes — trailing commas, mismatched brackets, incorrect quote types, and a handful of others.
Once you know what to look for, most JSON errors take just seconds to spot and fix, especially with the help of a good validator.
The next time your JSON throws an error, save yourself the guesswork — paste it into the free JSON Formatter tool on webgigo.com to instantly locate and fix the problem.