What is JSON?
JSON (JavaScript Object Notation) is the standard data format for API communication. It's lightweight, human-readable, and easy to parse.
JSON Syntax
{
"string": "hello",
"number": 42,
"float": 3.14,
"boolean": true,
"null": null,
"array": [1, 2, 3],
"object": {
"nested": "value"
}
}
JSON Data Types
| Type | Example | Notes |
|---|---|---|
| String | "hello" | Double quotes required |
| Number | 42, 3.14 | No distinction between int/float |
| Boolean | true, false | Lowercase |
| Null | null | Represents absence |
| Array | [1, 2, 3] | Ordered list |
| Object | {"key": "val"} | Key-value pairs |
JSON in API Communication
# Request
POST /api/users HTTP/1.1
Content-Type: application/json
{
"name": "Alice",
"email": "alice@example.com",
"preferences": {
"theme": "dark",
"notifications": true
}
}
# Response
HTTP/1.1 201 Created
Content-Type: application/json
{
"id": 123,
"name": "Alice",
"email": "alice@example.com",
"createdAt": "2026-08-15T10:30:00Z"
}
JSON Best Practices
- Use consistent naming — camelCase or snake_case (pick one)
- Always include Content-Type: application/json
- Use ISO 8601 for dates — "2026-08-15T10:30:00Z"
- Return meaningful error responses — Include error code and message
- Never return sensitive data — Passwords, tokens, secrets
JSON vs XML
| Feature | JSON | XML |
|---|---|---|
| Readability | High | Medium |
| Size | Smaller | Larger |
| Parsing | Easier | Complex |
| Schema | JSON Schema | XSD |
| API Usage | REST, GraphQL | SOAP, legacy |
Best Practices
Key Principles
- Follow SOLID principles
- Write clean, readable code
- Test thoroughly
- Document decisions
- Monitor in production
Implementation
- Start simple, refactor as needed
- Use established patterns
- Consider trade-offs
- Review with peers
Continuous Improvement
- Learn from incidents
- Update documentation
- Share knowledge
- Mentor others
Key Points
- Understanding JSON is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Practice Problems
Design and implement a solution for JSON in a backend system. Consider scalability, error handling, and production readiness.
Solution
// JSON implementation
// Key aspects: validation, error handling, logging, testing
public class JSON {
// Production-ready implementation
} Identify and handle edge cases for JSON. What happens under high load, with invalid input, or during failures?
Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotency Write a testing strategy for JSON. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injection Quiz
1. Which is the correct way to represent a string in JSON?
2. What is the recommended date format for JSON APIs?
3. What is a common mistake when implementing JSON?
Flashcards
Question
What is JSON?
Click to reveal answer
Answer
JavaScript Object Notation — lightweight data interchange format
Question
JSON string rules?
Click to reveal answer
Answer
Must use double quotes — 'hello' is invalid, "hello" is valid
Question
JSON best practices
Click to reveal answer
Answer
Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.
Revision Notes
Key Takeaways
- 1. JSON is the standard format for API communication
- 2. Strings require double quotes
- 3. Use ISO 8601 for dates
- 4. Always set Content-Type: application/json
Interview Tips
- • Know JSON syntax rules
- • Understand why JSON is preferred over XML
Cheat Sheet
JSON
- Format: {"key": "value"}
- Strings: Double quotes only
- Dates: ISO 8601 (2026-08-15T10:30:00Z)
- API: Always Content-Type: application/json
- vs XML: JSON is lighter, easier to parse