HTTP Status Codes with JSON β Interview Questions
β‘ 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.
βCoffee Chat Question
Concept Made Simple
βHow do HTTP status codes work together with JSON responses?β
π§ Mind Map Answer
Remember It Faster
Keep one consistent error envelope across the whole API so clients parse errors the same way everywhere.
β¨οΈHands-on Keyboard
Learn by Doing
HTTP/1.1 422 Unprocessable Entity
Content-Type: application/json
{
"error": "validation_failed",
"message": "Email is invalid",
"fields": { "email": "must be a valid email" }
}π₯What If?
Think Beyond the Expected
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:
β οΈ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
- 1What's the difference between 400 and 422?
- 2How do you design a consistent error schema?
- 3Which errors should be retried by clients?
π§©Related Technologies
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.
Was this answer helpful?
β Featured Products
Support our platform by exploring our recommended products.
As an Amazon affiliate, purchases through these links may earn us a small commission β at no extra cost to you. It helps keep Full Stack Interview Guru free.