10 JSON Validation Mistakes to Avoid
January 15, 2025 7 min read
1. Using Single Quotes
❌ Wrong:
✅ Correct:
{'name': 'John'}✅ Correct:
{"name": "John"}
JSON requires double quotes for both keys and string values.
2. Trailing Commas
❌ Wrong:
✅ Correct:
{"name": "John", "age": 30,}✅ Correct:
{"name": "John", "age": 30}
Trailing commas are not allowed in JSON (unlike JavaScript).
3. Unquoted Keys
❌ Wrong:
✅ Correct:
{name: "John"}✅ Correct:
{"name": "John"}
4. Using undefined
❌ Wrong:
✅ Correct:
{"value": undefined}✅ Correct:
{"value": null}
JSON doesn't support undefined. Use null instead.
5. Comments in JSON
❌ Wrong:
✅ Correct:
{"name": "John" /* comment */}✅ Correct:
{"name": "John"}
Standard JSON doesn't support comments. Use JSON5 if you need comments.
6. Not Validating Before Parsing
// Always use try-catch
try {
const data = JSON.parse(jsonString);
} catch (error) {
console.error('Invalid JSON:', error);
}
7. Trusting External Data
Always validate structure and types after parsing:
const data = JSON.parse(jsonString);
if (typeof data.age !== 'number') {
throw new Error('Invalid data type');
}
8. Ignoring Character Encoding
Always use UTF-8 encoding for JSON. Specify charset in HTTP headers:
Content-Type: application/json; charset=utf-8
9. Not Handling Large Numbers
JavaScript's Number type has limits. Large integers may lose precision:
// Use strings for large numbers
{"id": "9007199254740993"}
10. Forgetting to Escape Strings
Special characters must be escaped:
{"text": "Line 1\\nLine 2\\tTabbed"}
Best Practices
- Use a linter (ESLint, JSONLint)
- Validate against JSON Schema
- Use TypeScript for type safety
- Test with edge cases
Pro Tip: Use our JSON Validator to catch these mistakes automatically!
Back to Blog