Understanding “Malformed JSON – Illegal Character Encountered – JSON Syntax Error” and How to Fix It
JSON (JavaScript Object Notation) has become one of the most widely used data-interchange formats for APIs, configuration files, automation workflows, and system integrations. Its simplicity and readability make it ideal for developers, but even minor mistakes in formatting can lead to parsing failures.
One of the most common issues developers encounter is the error:
“Malformed JSON – Illegal character encountered – JSON syntax error.”
This article explains what the error means, the most frequent causes, and how to fix it effectively. It is written in a clear, SEO-friendly structure to help engineers quickly resolve JSON-related issues.
What Does “Malformed JSON” Mean?
A “malformed JSON” message indicates that the JSON input does not follow the strict rules of JSON syntax. JSON parsers expect a predictable structure, and a single incorrect character or formatting mistake can break the entire document.
When the message includes “Illegal character encountered,” it means the parser found a symbol or character that is not allowed in JSON.
Common Causes of Illegal Characters in JSON
1. Smart Quotes (“ ”) or Non-Standard Characters
Copy-pasting from Word, email, chat apps, or PDFs often introduces curved quotes or invisible characters that JSON cannot read.
Example of invalid quotes:
{ “name”: “John Doe” }
Valid JSON:
{ "name": "John Doe" }
2. Single Quotes Instead of Double Quotes
JSON only supports double quotes for keys and string values.
✘ Invalid:
{ 'name': 'John' }
✔ Valid:
{ "name": "John" }
3. Trailing Commas
A common mistake is leaving a comma at the end of the last element.
✘ Invalid:
{ "name": "John", }
✔ Valid:
{ "name": "John" }
4. Unescaped Characters
Characters like backslashes (\), line breaks, or control characters must be escaped properly.
✘ Invalid:
{ "path": "C:\newfolder" }
✔ Valid:
{ "path": "C:\\newfolder" }
5. Mismatched or Missing Braces
Missing {, }, [, or ] makes the entire JSON unreadable.
✘ Invalid:
{ "name": "John"
✔ Valid:
{ "name": "John" }
6. Wrong File Encoding
JSON files must use UTF-8 encoding. Other encodings can insert invisible illegal characters.
How to Fix Illegal Character and Syntax Errors in JSON
1. Validate the JSON Structure
Use online JSON validators or formatters such as:
- JSONLint
- JSON Formatter & Validator (CuriousConcept)
- Code editors with built-in linting (VSCode, IntelliJ, PyCharm)
These tools highlight the exact line and character causing the issue.
2. Replace Smart Quotes With Standard Double Quotes
Convert any curved or angled quotes into " ".
In VSCode:
- Press Ctrl + H
- Replace
“with" - Replace
”with"
3. Remove Trailing Commas
Ensure the last item in objects or arrays does not end with a comma.
4. Escape Special Characters Properly
Example of correct escaping:
\ → \\
" → \"
5. Fix Bracket and Brace Pairs
Make sure all opening brackets have matching closing brackets.
Modern tools like VSCode or JetBrains editors help detect mismatches visually.
6. Convert File Encoding to UTF-8
If the JSON file contains unreadable characters:
In VSCode:
- Click the encoding indicator at the bottom right
- Select Save with Encoding → UTF-8
Linux command:
iconv -f ISO-8859-1 -t UTF-8 input.json > output.json
Best Practices to Prevent JSON Syntax Errors
- Always use a code editor instead of writing JSON in word processors.
- Enable auto-formatting and linting in your IDE.
- Validate JSON before sending it to APIs.
- Avoid manual editing in email or chat applications.
- Use schema validation (e.g., JSON Schema) for complex structures.
Conclusion
The error “Malformed JSON – Illegal character encountered – JSON syntax error” is usually caused by formatting mistakes, invalid characters, or incorrect encoding. Fortunately, the issue is easy to diagnose and fix using proper validation tools, correct character usage, and consistent encoding practices.
Understanding how JSON structure works and applying best practices will help you avoid these errors and maintain smooth communication between your applications, APIs, and automation systems.
The simple tools for JSON format, validate and beautify can be found on Online JSON Formatter & Validator tools.











