JSON Validation β Interview Questions
β‘ Short Answer
Validation has two layers: syntactic (is it valid JSON at all β the parser checks this) and semantic (does it have the required fields, types, and constraints your API expects). You enforce the second with schema/validators (JSON Schema + Ajv, Zod, class-validator, Bean Validation) and reject bad input with a 400/422 listing the failures.
βCoffee Chat Question
Concept Made Simple
βHow do you validate incoming JSON?β
π§ Mind Map Answer
Remember It Faster
β¨οΈHands-on Keyboard
Learn by Doing
import { z } from "zod";
const User = z.object({
name: z.string().min(1),
email: z.string().email(),
age: z.number().int().positive().optional(),
});
const result = User.safeParse(JSON.parse(body));
if (!result.success) return res.status(422).json(result.error.issues);π₯What If?
Think Beyond the Expected
Isn't a successful JSON.parse enough to trust the data?
No. Parsing only proves the syntax is valid JSON β it says nothing about whether required fields exist, types are correct, or values are in range. Trusting parsed-but-unvalidated input leads to crashes and security holes (injection, mass assignment). Always validate the shape/semantics before using the data.
πReal World
Every serious backend validates request bodies against a schema before touching business logic β Zod/Joi in Node, class-validator in NestJS, Bean Validation in Spring. It's both a correctness and a security control at the trust boundary.
π―Interviewer's Expectation
Keywords they're listening for:
β οΈCommon Mistakes
- βTreating a successful parse as trusted data
- βValidating in business logic instead of at the boundary
- βVague error responses that don't say what failed
β Best Practices
- βValidate at the edge, before business logic
- βUse a schema/validator, not ad-hoc ifs
- βReturn specific field-level errors
πFollow-up Questions
- 1What is JSON Schema and how does it help?
- 2Why isn't parsing the same as validating?
- 3How do you return useful validation errors?
π§©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: Validation (JSON) Interview question: "How do you validate incoming 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?
β 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.