MediumExperience level: 3-5 years · 8-15 years1 min read

Handling Invalid JSON — Interview Questions

Reviewed by Gurusankar M.

Asked inAmazonMicrosoftDeloitteAccenture
#json#error handling#invalid json#try catch#validation
Report issue

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

javascript
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);
Output
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

  1. Why is malformed input a 400 and not a 500?
  2. How does JSON Schema fit into validation?
  3. 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.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.