EasyExperience level: 0-2 years · 3-5 years2 min read

HTTP Status Codes with JSON — Interview Questions

Reviewed by Gurusankar M.

Asked inAmazonMicrosoftDeloitteAccenture
#json#http status codes#error handling#rest#api design
Report issue

Short Answer

The status code carries the outcome (2xx success, 4xx client error, 5xx server error) and the JSON body carries the detail. Success bodies hold the resource; error bodies hold a consistent error shape (code, message, maybe field errors). Anti-pattern: returning 200 with {"error":...} — clients then can't rely on the status code.

On this page

Concept Made Simple

Coffee Chat Question

“How do HTTP status codes work together with JSON responses?”

Remember It Faster

Mind Map Answer

2xx + JSON
resource / result body
400/422 + JSON
validation error details
401/403 + JSON
auth error message
5xx + JSON
generic error (no internals)

Keep one consistent error envelope across the whole API so clients parse errors the same way everywhere.

Learn by Doing

Hands-on Keyboard

http
HTTP/1.1 422 Unprocessable Entity
Content-Type: application/json

{
  "error": "validation_failed",
  "message": "Email is invalid",
  "fields": { "email": "must be a valid email" }
}

Think Beyond the Expected

What If?

Why is returning HTTP 200 with an error body considered a bad practice?

It breaks the contract clients, proxies, caches, and monitoring rely on. Tooling treats 2xx as success, so a 200 'error' won't trigger retries, alerts, or error handling — the failure hides. Use the correct 4xx/5xx status AND a JSON error body: the code for machines, the body for humans/details.

Real World

Mature APIs define one error envelope (code, message, requestId) and always pair it with the right status. Front-ends branch on res.ok/status; observability tools alert on 5xx rates — all of which relies on honest status codes.

Interviewer's Expectation

Keywords they're listening for:

  • status = outcome, body = detail
  • 2xx/4xx/5xx families
  • consistent error envelope
  • don't return 200 for errors
  • hide internals in 5xx

Common Mistakes

  • Returning 200 for failures
  • Inconsistent error shapes across endpoints
  • Leaking stack traces in 5xx bodies

Best Practices

  • Match status code to the real outcome
  • Standardize one error envelope
  • Return generic 5xx bodies (no internals)

Follow-up Questions

  1. What's the difference between 400 and 422?
  2. How do you design a consistent error schema?
  3. Which errors should be retried by clients?

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: REST APIs (JSON)
Interview question: "How do HTTP status codes work together with JSON responses?"

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.