MediumπŸ‘€ 3-5 yearsπŸ‘€ 8-15 years 1 min read

JSON Validation β€” Interview Questions

Asked inAmazonMicrosoftDeloitteCognizant
#json#validation#json schema#zod#input validation
Report issue

⚑ 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

Syntactic→valid JSON? (parser)
Semantic→right fields/types/ranges?
Tools→JSON Schema/Ajv, Zod, Bean Validation
On fail→400/422 + field errors

⌨️Hands-on Keyboard

Learn by Doing

javascript
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:

βœ“ 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

  • 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

JSON SchemaZod/Joi/AjvBean ValidationOpenAPI

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?

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.

Related Questions