JSON Validation — Interview Questions
Reviewed by Gurusankar M.
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.
On this page
Concept Made Simple
Coffee Chat Question
“How do you validate incoming JSON?”
Remember It Faster
Mind Map Answer
- Syntactic
- valid JSON? (parser)
- Semantic
- right fields/types/ranges?
- Tools
- JSON Schema/Ajv, Zod, Bean Validation
- On fail
- 400/422 + field errors
Learn by Doing
Hands-on Keyboard
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);Think Beyond the Expected
What If?
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:
- syntactic vs semantic
- required fields/types/constraints
- schema validators
- parse ≠ valid
- reject with 400/422
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
- What is JSON Schema and how does it help?
- Why isn't parsing the same as validating?
- How do you return useful validation errors?
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?
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