Formatters

How Modern APIs Use JSON: REST, GraphQL, WebSockets, and Streaming

Explore how production APIs leverage JSON: HTTP content negotiation, RFC 7807 problem details, pagination strategies, and NDJSON streaming.

WebUtil Team

1. JSON as the Universal Language of Modern Distributed Web Services

From cloud-native microservices on Kubernetes to mobile app backends and public developer platforms (Stripe, GitHub, AWS), JSON is the unquestioned universal data exchange standard of the modern internet. Its native compatibility with JavaScript, human readability, and lightweight footprint make it the default transport format for RESTful architectures, GraphQL queries, and WebSocket messaging.

2. Content Negotiation: Content-Type and Accept Headers

HTTP content negotiation ensures that clients and servers agree on data serialization. When sending or requesting JSON payloads, proper headers must be specified:

  • `Content-Type: application/json; charset=utf-8`: Informs the recipient that the HTTP request or response body contains JSON encoded in UTF-8.
  • `Accept: application/json`: Informs the server that the client only accepts JSON responses. If the server cannot provide JSON, it should return an HTTP 406 Not Acceptable status code.

3. Predictable Error Envelopes: RFC 7807 Problem Details

Standardizing API error structures prevents client applications from breaking unexpectedly. RFC 7807 defines 'Problem Details for HTTP APIs' using media type `application/problem+json`:


              {
  "type": "https://webutil.tech/errors/invalid-token",
  "title": "Authentication Token Expired",
  "status": 401,
  "detail": "The provided JSON Web Token expired at 2026-10-17T14:00:00Z.",
  "instance": "/api/v1/user/account",
  "invalid_params": [{ "name": "Authorization", "reason": "Signature timestamp expired" }]
}
            
Sponsored
Advertisement

4. API Pagination Patterns in JSON: Offset vs Cursor-Based Structures

When returning large collections, APIs must paginate responses:

  • Offset Pagination: Uses `page=2&limit=20`. Simple to implement, but suffers from performance degradation on deep database pages (`OFFSET 100000`) and can return duplicate records if new items are inserted during pagination.
  • Cursor-Based Pagination: Uses an opaque cursor (typically a base64-encoded timestamp or UUID). Highly performant, immune to phantom inserts, and recommended for infinite-scroll feeds.

5. Real-Time Streaming with NDJSON and Server-Sent Events

Standard JSON parsers require the entire document to be downloaded before parsing can begin. For large data exports or real-time LLM AI completions (like ChatGPT or Claude), modern APIs use Newline Delimited JSON (NDJSON) or Server-Sent Events (SSE):


              {"event": "token", "text": "The"}
{"event": "token", "text": " future"}
{"event": "token", "text": " of"}
{"event": "token", "text": " AI"}
            

Each line is an independent, valid JSON document separated by a newline (`\n`), allowing clients to stream, parse, and render output token by token without buffering gigabytes into memory.

6. Security Hardening: Defending Against Prototype Pollution

When merging untrusted JSON payloads into existing objects, attackers can exploit `__proto__` and `constructor` keys to pollute global object prototypes in JavaScript. Always sanitize input keys or use `Object.create(null)` for map lookups.