Handling Invalid JSON — Interview Questions
Reviewed by Gurusankar M.
Short Answer
Never assume input is valid: wrap parsing in try/catch (or a safeParse helper that returns a result/null), verify HTTP status and Content-Type first, and after parsing validate the shape (required fields/types) before use. On failure, log the raw payload and return a clear error (e.g. 400) rather than letting the parser throw uncaught.
On this page
Concept Made Simple
Coffee Chat Question
“How do you handle invalid or malformed JSON?”
Remember It Faster
Mind Map Answer
- Guard
- check status + Content-Type
- Parse
- try/catch or safeParse → result
- Validate
- shape/types after parse
- Fail
- log raw + return clear 400 error
Learn by Doing
Hands-on Keyboard
function safeParse(text) {
try {
return { ok: true, data: JSON.parse(text) };
} catch (e) {
return { ok: false, error: e.message };
}
}
const r = safeParse('{bad json');
console.log(r.ok ? r.data : "Invalid JSON: " + r.error);Invalid JSON: Unexpected token b ...
Think Beyond the Expected
What If?
Should a public API crash or return a 500 when it receives malformed JSON?
Neither — return 400 Bad Request with a clear message. Malformed input is a client error, not a server fault, so a 500 is misleading and an uncaught throw can take down the request handler. Parse defensively, and respond with 400 plus enough detail for the caller to fix their payload.
Real World
Robust services treat every inbound body as hostile: guard, safe-parse, validate, and respond with a precise 400. This is also a security posture — malformed or oversized JSON is a common probing technique.
Interviewer's Expectation
Keywords they're listening for:
- try/catch / safeParse
- check status & content-type
- validate shape after parse
- return 400 not 500
- log raw payload
Common Mistakes
- Letting JSON.parse throw uncaught
- Returning 500 for a client's bad JSON
- Skipping shape validation after a successful parse
Best Practices
- Safe-parse + validate at every boundary
- Return 400 with actionable messages
- Cap request body size
Follow-up Questions
- Why is malformed input a 400 and not a 500?
- How does JSON Schema fit into validation?
- How do you guard against oversized payloads?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Parsing (JSON) Interview question: "How do you handle invalid or malformed JSON?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair