
JSON Debugging: How I Stopped Losing My Mind Over Missing Commas
Real stories from the JSON debugging trenches. Learn from my pain as we explore common errors, validation tricks, and why that one comma cost me 2 hours of my life.
The Trailing Comma That Ruined My Tuesday
It was 3 AM. Not my proudest moment, but production was down and I was the one holding the pager.
Critical API integration failing. Error message? "Invalid JSON." Helpful, right?
I stared at 400 lines of config for what felt like forever. Checked every quote. Verified every bracket. Ran it through three different validators. Everything looked perfect.
The culprit? A stupid trailing comma after the last array element.
{
"settings": {
"enabled": true,
"timeout": 30,
"retries": 3,
}
}
See that comma after the 3? Yeah. That little bastard cost me two hours of sleep and most of my dignity.
If you're reading this and thinking "I would never make that mistake," congrats! You're either a better developer than me or you haven't worked with enough JSON yet. Give it time. It'll get you too.
Why JSON Is Both Amazing and Terrible
JSON is everywhere. And I mean EVERYWHERE:
- REST APIs? JSON.
- Config files? JSON.
- NoSQL databases? JSON.
- That weird API your client insists on using? Probably JSON.
It's the lingua franca of modern web development. But here's the thing: JSON was designed for machines, not humans. It's strict. Unforgiving. One tiny syntax error and the whole thing breaks.
According to Stack Overflow's survey, debugging is consistently one of the most frustrating parts of development. And JSON formatting issues? Way more common than you'd think. The syntax is rigid but the error messages are vague. Classic combination.
JSON Syntax: The Rules That Break Everything
JSON has exactly six data types. That's it. Learn them once, use them forever.
The Data Types (Quick Refresher)
| Type | Example | Gotchas |
|------|---------|---------|
| String | "Hello" | Double quotes ONLY. No single quotes. |
| Number | 42, 3.14 | No leading zeros (except 0.x). No quotes. |
| Boolean | true, false | Lowercase ONLY. Not True or TRUE. |
| Null | null | Lowercase. Not NULL or None. |
| Array | [1, 2, 3] | Ordered. Can mix types (but don't). |
| Object | {"key": "val"} | Keys MUST be quoted. |
The Syntax Rules That'll Get You
Rule #1: Double Quotes or GTFO
JavaScript is chill about quotes. JSON is not.
// ❌ Nope
{ 'name': 'John' }
// ✅ Yes
{ "name": "John" }
Rule #2: No Trailing Commas (The One That Got Me)
JavaScript allows trailing commas. JSON doesn't. At all.
// ❌ This will fail
[1, 2, 3,]
// ✅ This won't
[1, 2, 3]
I know. It's annoying. I've petitioned to change it. Nobody listened.
Rule #3: Quote Your Keys
Objects in JavaScript don't need quoted keys. JSON requires them.
// ❌ JavaScript object, not JSON
{ name: "John" }
// ✅ Actual JSON
{ "name": "John" }
Rule #4: No Comments
You can't add comments to JSON. I know, I know. You want to explain that weird config setting. You can't. Not in pure JSON.
// ❌ Invalid
{
// Set timeout to 30 seconds
"timeout": 30
}
Workaround: Use a "_comment" field if you must.
// ✅ Valid (kinda)
{
"_comment": "Set timeout to 30 seconds",
"timeout": 30
}
Rule #5: Escape Special Characters
Quotes inside strings need escaping. Backslashes need escaping.
// ❌ Nope
{ "message": "He said "Hello"" }
// ✅ Yep
{ "message": "He said \"Hello\"" }
The 10 Most Common JSON Errors (AKA My Greatest Hits)
1. Trailing Commas (40% of errors)
I've done this. You've done this. We've all done this.
// ❌ The classic mistake
[1, 2, 3,]
// ✅ The right way
[1, 2, 3]
2. Single Quotes
Coming from Python or JavaScript? This'll bite you.
// ❌ Invalid
{'key': 'value'}
// ✅ Valid
{"key": "value"}
3. Unquoted Keys
// ❌ Nope
{key: "value"}
// ✅ Yep
{"key": "value"}
4. Undefined Values
JavaScript has undefined. JSON says "nah."
// ❌ Invalid
{"value": undefined}
// ✅ Use null
{"value": null}
5. Comments
Still mad about this one.
// ❌ Invalid
{
// Configuration
"timeout": 30
}
6. Unescaped Quotes
// ❌ Breaks everything
{"message": "He said "Hello""}
// ✅ Escaped properly
{"message": "He said \"Hello\""}
7. BOM Characters
Some editors add invisible characters at the start of files. This breaks parsers silently. Save as "UTF-8 without BOM" to avoid this nightmare.
8. Line Breaks in Strings
// ❌ Invalid
{"text": "Line 1
Line 2"}
// ✅ Use \n
{"text": "Line 1\nLine 2"}
9. NaN and Infinity
JavaScript has these. JSON doesn't.
// ❌ Invalid
{"result": NaN}
// ✅ Use null or string
{"result": null}
{"result": "NaN"}
10. Invalid Unicode
Malformed escape sequences break everything.
// ❌ Incomplete surrogate pair
{"emoji": "\uD83D"}
// ✅ Complete pair
{"emoji": "\uD83D\uDE00"}
JSON vs JavaScript Objects: Know the Difference
This confuses everyone at first. JSON looks like JavaScript, but it's not.
| Feature | JSON | JavaScript | |---------|------|------------| | Quotes | Double only | Single or double | | Keys | Must be quoted | Can be unquoted | | Trailing commas | NO | Yes | | Comments | NO | Yes | | undefined | NO | Yes | | Functions | NO | Yes |
Converting Between Them
// Object → JSON String
const obj = { name: "John", age: 30 };
const jsonString = JSON.stringify(obj);
// '{"name":"John","age":30}'
// JSON String → Object
const parsed = JSON.parse(jsonString);
// { name: "John", age: 30 }
My 30-Second Debugging Workflow
After years of JSON-induced suffering, here's what I actually do:
- Copy the problematic JSON (from logs, API response, wherever)
- Paste into a formatter (like our JSON Formatter)
- Look for red error highlighting
- Fix the syntax issue (usually obvious once formatted)
- Minify if needed and paste back
Takes under a minute. Beats staring at minified JSON for hours.
Real-World Debugging Scenarios
Scenario 1: API Returns "Invalid JSON"
What I do:
- Log the exact request body
- Paste into formatter
- Check for trailing commas (it's always trailing commas)
- Verify all quotes are double
- Look for unescaped characters
Prevention:
// Let JavaScript handle the formatting
fetch('/api/data', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data) // Never manually stringify
});
Scenario 2: Config File Won't Load
Debug steps:
- Validate with online linter
- Check file encoding (UTF-8, no BOM)
- Verify file actually has content (empty files are valid JSON... technically)
- Test minimal config first, add fields one by one
Command line validation:
# Quick check
node -e "JSON.parse(require('fs').readFileSync('config.json'))"
# Using jq (if you have it)
jq . config.json
Scenario 3: Large JSON File Crashes Everything
When 50MB of JSON breaks your editor:
- Use streaming parsers - Don't load it all into memory
- Consider JSONL - JSON Lines format for big datasets
- Split into chunks - One file per record type
- Compress it - Gzip for transmission
// Streaming with Node.js
const fs = require('fs');
const JSONStream = require('JSONStream');
fs.createReadStream('huge-file.json')
.pipe(JSONStream.parse('*'))
.on('data', item => {
// Process one item at a time
});
Tools That Actually Help
Online Tools (Privacy-Safe)
- Runs 100% in your browser (your data stays yours)
- Syntax highlighting
- Error detection with line numbers
- Minify and beautify
I built this because I got tired of other tools uploading my data to servers. When you're debugging production configs, privacy matters.
Command Line
jq - The JSON processor everyone should know:
# Pretty print
jq . data.json
# Extract field
jq '.users[0].name' data.json
# Filter array
jq '.users[] | select(.age > 18)' data.json
Python (if you have it):
python -m json.tool data.json
IDE Extensions
VS Code:
- Built-in JSON validation
- Prettify command (Shift+Alt+F)
- Schema support
WebStorm/IntelliJ:
- Excellent JSON support out of the box
- Real-time validation
Security Stuff You Should Know
JSON Injection (Don't Concatenate User Input)
// ❌ Dangerous
const json = `{"message": "${userInput}"}`;
// ✅ Safe
const obj = { message: userInput };
const json = JSON.stringify(obj);
Prototype Pollution
Malicious JSON can mess with Object.prototype:
{
"user": "admin",
"__proto__": {
"isAdmin": true
}
}
Fix:
// Use Object.create(null)
const data = JSON.parse(json);
const safeData = Object.assign(Object.create(null), data);
Never Use JSONP
It's 2026. JSONP is deprecated and dangerous. Use CORS instead.
FAQ: Questions From Actual Humans
Q: Why doesn't JSON allow comments?
A: Douglas Crockford (JSON's creator) wanted to keep it simple. Use "_comment" fields if you must.
Q: Can I use JSON for config files? A: You can, but consider YAML or JSON5 if you need comments.
Q: What's the maximum JSON size? A: No hard limit, but practical limits exist. Browsers struggle past 2-4MB.
Q: Should I use JSON for huge datasets? A: Consider Protocol Buffers or MessagePack for large data.
Q: How do I pretty print JSON in terminal?
A: jq . file.json or python -m json.tool file.json
Q: Can JSON handle binary data?
A: Not directly. Encode as Base64: {"image": "iVBORw0KGgo..."}
The Bottom Line
JSON is simple but unforgiving. One missing comma, one single quote, one unescaped character—and everything breaks.
The good news? With the right tools and a bit of practice, debugging JSON becomes second nature. The bad news? You'll still occasionally lose hours to a trailing comma at 3 AM. It's a rite of passage.
My advice:
- Use a formatter/validator religiously
- Never trust user-provided JSON
- Let
JSON.stringify()handle formatting instead of doing it manually - Keep a JSON linter bookmarked
And maybe, just maybe, double-check for trailing commas before pushing to production. Trust me on this one.
Ready to validate? Try our JSON Formatter — it catches all the mistakes I've talked about and probably a few I haven't discovered yet.
Happy debugging. May your JSON always be valid.
Written at 3 AM by someone who really should've checked for that trailing comma
Written by Axonix Team
Axonix Team - Technical Writer @ Axonix
Share this article
Discover More
View all articles
PocketMCP: Turn Your Android Phone into an AI Agent Server
Learn how the open-source PocketMCP app uses the Model Context Protocol to bridge the gap between desktop AI agents and real-world mobile data.

React 19: Practical Features That Actually Matter
React 19 is here and it's not just hype. After building with it for two months, here's what actually matters and what you can ignore.

JWTs Explained: No More Auth Headaches
JSON Web Tokens are the standard for stateless authentication. But how do they actually work? We break down the header, payload, and signature so you can implement auth securely.
Use These Related Tools
View all toolsReady to boost your productivity?
Axonix provides 20+ free developer tools to help you code faster and more securely.
Explore Our Tools