How to Read and Fix Common JSON Errors
You paste a block of JSON into your application, but the parser throws an error. The message looks confusing. You scan 200 lines and still cannot find the problem.
This is a common frustration when working with APIs, configuration files, or data exchanged between applications. Fortunately, most JSON syntax errors come from a small number of mistakes.
This guide explains how to read JSON error messages, identify common syntax problems, and fix invalid JSON step by step. You will also learn how to use an online JSON validator, check files offline, and avoid errors when generating JSON in code.
JSON Syntax Rules You Need to Know
Understanding a few basic rules makes JSON errors much easier to recognize.
The Core JSON Syntax Rules
JSON has a strict syntax. Keep these rules in mind when writing or editing a JSON file:
- Keys must use double quotes: Object property names must be strings enclosed in double quotes.
- Strings require double quotes: Single-quoted strings are invalid in standard JSON.
- Values must use valid JSON types: These include strings, numbers, booleans,
null, arrays, and objects. - Commas separate items: Object properties and array elements require commas between them.
- Trailing commas are not allowed: The final property or element must not be followed by an extra comma.
- Braces and brackets must match: Objects use
{}and arrays use[]. - Comments are not allowed: Standard JSON does not support
//or/* ... */comments. - Booleans and null are lowercase: Use
true,false, andnull, notTrue,False, orNone.
Here is a valid JSON example:
{
"name": "Ava",
"age": 29,
"skills": ["writing", "design"],
"active": true
}
The example contains an object with four properties. The values include a string, a number, an array, and a boolean. Every key is quoted correctly, and the commas and brackets are in the right places.
Why Malformed JSON Happens
Malformed JSON usually results from manual editing, copy-and-paste mistakes, incorrect data, or code that builds JSON strings without using a proper serializer.
JavaScript objects and standard JSON look similar, but they are not interchangeable. JavaScript object literals can use unquoted property names, single-quoted strings, and trailing commas in many contexts. Standard JSON does not allow these shortcuts.
For example, this is valid JavaScript object-literal syntax but invalid JSON:
{
name: 'Ava',
age: 29,
}
The equivalent valid JSON is:
{
"name": "Ava",
"age": 29
}
When an application expects JSON, even a small syntax difference can cause parsing to fail.
How to Read JSON Errors
Understand JSON Error Messages
A JSON parser error may identify the problem, a character position, or a line and column number. The details depend on the language, parser, and runtime.
Here are examples of common error messages:
Unexpected token } in JSON at position 52Expecting ',' delimiter: line 4 column 3Unexpected end of JSON inputUnterminated string starting at: line 2 column 10
These are illustrative examples, not guaranteed messages from every parser.
Start by reading the full error message. If it provides a line, column, or character position, use that information to locate the problem.
Next, inspect the surrounding text, especially the characters immediately before the reported position. The parser often reports where it detected the problem rather than where the mistake began.
For example, a missing comma after a property may cause the parser to complain when it encounters the next property name.
Know Your Parser
Different programming languages and tools describe JSON errors differently.
JavaScript may report an unexpected token or an unexpected end of input. Python’s JSON decoder may report a missing delimiter together with a line and column.
The wording is less important than the underlying issue. Look for a missing comma, an unclosed string, an invalid value, or a bracket that does not match.
If the error includes a character position, remember that different tools may count positions differently. Some use zero-based positions, while others provide line and column numbers.
Common JSON Errors and How to Fix Them
JSON Missing Comma Error
A missing comma occurs when two properties or array elements appear next to each other without the required separator.
Invalid JSON:
{
"name": "Ava"
"age": 29
}
The object is missing a comma after "Ava".
Corrected JSON:
{
"name": "Ava",
"age": 29
}
When a parser reports an unexpected token near a property name, check the previous property for a missing comma. Also make sure you have not added a comma where a colon should appear.
Trailing Comma Error
A trailing comma appears after the final property in an object or the final element in an array. Standard JSON does not allow it.
Invalid JSON:
{
"name": "Ava",
"age": 29,
}
Corrected JSON:
{
"name": "Ava",
"age": 29
}
Remove the comma after 29. The same rule applies to arrays.
Invalid array:
["writing", "design",]
Corrected array:
["writing", "design"]
Trailing commas are easy to leave behind when deleting or moving properties. Always inspect the last item in an object or array.
JSON Unexpected Token Error
An unexpected token error means the parser encountered text that does not fit the expected JSON structure.
Common causes include:
- Single quotes around strings or keys.
- Unquoted property names.
- Missing commas or colons.
- Unexpected text before or after the JSON value.
- Unsupported values such as
undefinedorNaN. - HTML returned instead of JSON.
- A malformed string or mismatched bracket.
Consider this invalid JSON:
{
"name": 'Ava'
}
The string uses single quotes. Change them to double quotes.
Another common case occurs when an API request returns an HTML error page instead of JSON. If the response begins with something like <html>, the problem may be the server response, an incorrect URL, or an authentication failure rather than the JSON parser itself.
Check the HTTP status code and response body before trying to repair the data.
A byte order mark (BOM) or other unexpected character at the start of a file can also cause problems in some parsers. However, not every parser handles BOMs the same way, so check the actual file and the parser’s behavior before assuming that is the cause.
JSON Bracket Mismatch
Every opening brace or bracket must have a corresponding closing character.
Invalid JSON:
{
"skills": ["writing", "design"
}
The array is missing its closing square bracket.
Corrected JSON:
{
"skills": ["writing", "design"]
}
If you see an error near a closing brace, check whether an array or nested object opened earlier has been closed properly.
Formatting the JSON with indentation makes its structure easier to inspect. Many code editors also highlight matching brackets, helping you identify missing or misplaced characters.
Unterminated Strings and Bad Escapes
Every JSON string must start and end with a double quote. Unescaped quotation marks and backslashes inside strings can make otherwise valid JSON fail.
For example, this is invalid:
{
"message": "She said "hello""
}
The quotation marks around hello end the string too early.
Corrected JSON:
{
"message": "She said \"hello\""
}
Use \" to include a double quote inside a string. Use \\ when the string needs a literal backslash.
For example, a Windows path should be written like this:
{
"path": "C:\\Users\\Ava"
}
A single backslash can introduce an escape sequence, such as \n for a newline or \t for a tab. If the backslash is meant to appear as an actual character, escape it.
JSON strings cannot contain unescaped literal line breaks. Use \n when you want a newline character within a string value.
Number and Value Errors
JSON supports numbers, but their syntax is more restrictive than many programming languages’ numeric formats.
These examples are invalid:
{
"count": 007,
"price": +12,
"rating": 4.
}
The issues are:
007has a leading zero.+12uses a leading plus sign.4.has no digit after the decimal point.
A corrected example is:
{
"count": 7,
"price": 12,
"rating": 4.0
}
Also remember that JSON does not support NaN, Infinity, or undefined as values.
Use lowercase JSON literals:
{
"enabled": true,
"archived": false,
"description": null
}
If your application has missing or undefined values, decide how they should be represented before generating JSON. You may need to omit a property or use null, depending on your data requirements.
Use a JSON Validator and Error Checker
Validate JSON Online
A JSON validator checks whether your text follows standard JSON syntax. Depending on the tool, it may display an error message, highlight the problematic area, or format the valid output.
An online JSON syntax checker can be useful when you need to inspect a configuration file, test an API response, or check a small JSON object quickly.
When choosing a validator, look for:
- Line and column information where available.
- Clear error messages.
- Formatting and minifying options.
- Support for the JSON features you actually need.
- A clear privacy policy and explanation of how your text is processed.
Remember that a syntax validator checks whether the JSON is structurally valid. It does not necessarily confirm that the data has the correct values, required properties, or expected types for your application.
For example, this is valid JSON:
{
"age": "twenty-nine"
}
But an application expecting age to be a number may still reject it. That is a data-validation or schema-validation problem, not a JSON syntax error.
Choose a Safe JSON Error Checker
Do not paste API keys, passwords, customer records, or other confidential data into an unfamiliar online tool.
Before using a validator, check whether it processes the data locally in your browser or sends it to a remote server. A privacy statement alone does not guarantee that processing occurs locally.
For sensitive information, use a trusted local editor, command-line validator, or development environment.
Validate JSON Offline
You can validate JSON without uploading it to a website. Here are three common options.
Option 1: Visual Studio Code
Open your .json file in Visual Studio Code. The editor can identify many JSON syntax errors and display diagnostic information as you edit. The exact messages and available features depend on your editor version and extensions.
Option 2: Python
If Python is installed, run:
python -m json.tool file.json
Replace file.json with your actual filename. The command parses the file and prints formatted JSON if it is valid. If parsing fails, Python reports an error.
To save formatted output to another file, use:
python -m json.tool file.json > formatted.json
Keep the input and output filenames different so you do not overwrite your original file.
Option 3: jq
If jq is installed, you can validate and format a JSON file with:
jq . file.json
The command prints the parsed JSON in a readable format when the input is valid. If the file contains invalid JSON, jq reports a parsing error.
These local methods are especially useful for large files or data that should not be uploaded to a third-party service.
A Simple JSON Troubleshooting Workflow
Use the same process whenever a JSON parser reports an error.
- Read the full error message. Note the position, line, column, or unexpected character if one is provided.
- Inspect the surrounding text. Check the reported location and the lines immediately before it.
- Format the JSON. Indentation makes nested objects and arrays easier to follow.
- Check commas and colons. Make sure properties and array elements are separated correctly.
- Check strings and escapes. Look for unclosed quotes, incorrect backslashes, and unsupported line breaks.
- Match every bracket. Confirm that all objects and arrays are properly closed.
- Validate again. Fix one problem at a time and rerun the parser or validator.
- Check the data source. If the error returns, inspect the API response or the code that generates the JSON.
A parser may stop at the first syntax error it encounters. After you fix that issue, another error may become visible. This is normal, so validate the file again after making changes.
If your application generates JSON dynamically, fix the code that produces it rather than repeatedly editing the output by hand.
For JavaScript, use JSON.stringify() to serialize supported values:
const data = {
name: "Ava",
age: 29
};
const jsonText = JSON.stringify(data);
console.log(jsonText);
For Python, use json.dumps():
import json
data = {
"name": "Ava",
"age": 29
}
json_text = json.dumps(data)
print(json_text)
Serializers handle the necessary quoting and escaping for supported data types. However, you should still check that the resulting data matches your application’s expected structure and requirements.
Conclusion
Most JSON syntax errors come from a familiar set of problems: missing commas, trailing commas, single quotes, mismatched brackets, unclosed strings, and invalid values.
Start by reading the parser’s error message, then inspect the reported position and surrounding text. Format the file to make its structure easier to understand, fix one issue at a time, and validate the result again.
An online JSON validator is convenient for non-sensitive data, while tools such as Visual Studio Code, Python, and jq let you work locally. If your application generates JSON, use a serializer instead of building JSON strings manually.
With a consistent debugging process, even a confusing JSON error becomes much easier to diagnose.
Frequently asked questions
How do I fix JSON errors quickly?
Read the error message, inspect the reported position and the preceding text, and look for missing commas, incorrect quotes, or mismatched brackets. Then validate the file again after fixing the problem.
What does “Unexpected token” mean in JSON?
It means the parser encountered a character or value that is not valid at that point in the JSON structure. Common causes include single quotes, missing commas, unquoted keys, and unexpected HTML or other text.
Why does my JSON say “Unexpected end of input”?
The parser reached the end of the input before it could finish reading the JSON value. A closing bracket, brace, or quotation mark may be missing, or the input may be incomplete or empty.
Can JSON have comments?
No. Standard JSON does not support comments. If you need comments in a configuration file, consider a format such as JSON5, provided the application you use supports it.
What is the difference between JSON and a JavaScript object?
JSON is a text-based data format with stricter syntax rules. JavaScript object literals can use unquoted property names, single-quoted strings, and trailing commas in many contexts, while standard JSON requires double-quoted property names and strings and does not permit trailing commas.
How can I check whether a JSON file is valid without an online tool?
Use a local editor such as Visual Studio Code, run python -m json.tool file.json, or use jq . file.json if jq is installed. These methods can check JSON syntax without uploading the file to an online validator.
Why is my JSON valid but my API still rejects it?
Valid syntax does not guarantee that the data meets the API’s requirements. The API may expect particular property names, data types, required fields, or a specific structure. Check the API documentation and its response message.
Is a trailing comma allowed in JSON?
No. Standard JSON does not allow a comma after the final property in an object or the final element in an array. Remove the extra comma to make the JSON valid.
How do I fix backslashes in a JSON file path?
Escape each literal backslash by writing it twice. For example, use "C:\\Users\\Ava" rather than "C:\Users\Ava". This prevents the backslashes from being interpreted as unintended escape sequences.
Why does my JSON validator show only one error at a time?
Many parsers stop when they encounter the first syntax error they cannot recover from. After you correct it, the parser may discover another problem later in the file. Fix errors one at a time and validate the JSON after each change.