JSON & Data Formats

JSON (JavaScript Object Notation) is the de facto standard for modern REST APIs. It is lightweight, easy to read, and natively supported by almost every language.

1. Consistent Envelope Pattern

Consistency is king. Every response should follow the same pattern.

// ✅ Successful Response
{
  "status": "success",
  "data": {
    "user": { "id": 1, "name": "Pradeep" }
  }
}

// ❌ Error Response
{
  "status": "error",
  "message": "Invalid credentials",
  "code": 401
}

2. Proper Date Handling

Always use ISO 8601 format for dates in JSON. It's unambiguous and sortable as text.

"created_at": "2023-11-20T14:30:00Z"

3. Snake_case vs CamelCase

While JavaScript prefers camelCase, many public APIs (and Python/PHP backends) use snake_case. Choose one and stick to it globally.

4. Handling Pagination

Don't just return a list. Return the "Metadata" about the list.

{
  "data": [...],
  "meta": {
    "total_pages": 15,
    "current_page": 1,
    "limit": 50,
    "total_results": 742
  }
}
Tip: Avoid returning null values for missing fields if possible. Simply omitting the field or returning an empty array/string often makes client-side logic cleaner.