Structuring Response Payloads

1. Structuring JSON Responses

ConventionExample
camelCase keys"firstName"
ISO 8601 dates"2026-05-15T10:30:00Z"
Numbers unquoted"price": 19.99
Booleans lowercase"active": true
No trailing commasJSON spec forbids

2. Using Response Envelopes

StyleProsCons
Bare resourceConcise, REST-pureNo room for metadata
Envelope (data/meta)Pagination/links inlineVerbose, extra nesting
JSON:APIStandardized, toolingHeavy, learning curve

Example: Envelope Format

{
  "data": [{"id": 1, "name": "Alice"}],
  "meta": {
    "page": 1,
    "perPage": 20,
    "total": 145
  },
  "links": {
    "self": "/users?page=1",
    "next": "/users?page=2"
  }
}

3. Including Resource Metadata

Metadata FieldPurpose
createdAt / updatedAtAudit timestamps
versionOptimistic locking
_links / _embeddedHAL hypermedia
etagConcurrency token

4. Handling Empty Responses

ScenarioResponse
Empty collection200 OK + {"data": []} (NOT 404)
Resource not found404 Not Found
Successful DELETE/PUT204 No Content (no body)

5. Including Pagination Metadata

FieldDescription
page / perPageCurrent page, page size
totalTotal record count
totalPagesTotal page count
hasNext / hasPrevNavigation booleans
nextCursorCursor pagination token

Example: HAL Format

{
  "id": 100,
  "amount": 250.00,
  "_links": {
    "self":   {"href": "/orders/100"},
    "user":   {"href": "/users/42"},
    "cancel": {"href": "/orders/100/cancel", "method": "POST"}
  }
}
Link FieldPurpose
selfCanonical URI of resource
relatedRelated resource
next / prevPagination

7. Structuring Error Responses

Use RFC 7807 Problem Details for HTTP APIs as the standard.

Example: RFC 7807 Problem Details

{
  "type": "https://example.com/errors/validation",
  "title": "Validation Failed",
  "status": 422,
  "detail": "Email is required and must be valid",
  "instance": "/users/42",
  "errors": [
    {"field": "email", "code": "REQUIRED", "message": "Email is required"}
  ],
  "traceId": "abc-123-def"
}

8. Using Consistent Naming Conventions

ConventionUseExample
camelCaseJSON keys (most common)userId, createdAt
snake_casePython, Ruby APIsuser_id, created_at
kebab-caseURL paths only/user-profiles

9. Handling Null vs Omitted Fields

ApproachMeaning
"field": nullField exists, no value
Omit fieldField unknown / not applicable / not requested
Empty string ""Distinct from null (e.g. cleared text)
Note: Be consistent. Document choice in API spec. Omitting reduces payload size.

10. Including Response Headers

HeaderPurpose
Content-TypeResponse format
Content-LengthBody size
ETagCache validation
Last-ModifiedCache validation
Cache-ControlCache directives
LocationCreated resource URI
X-Request-IDEcho correlation ID
X-RateLimit-*Rate limit status