application/x-ndjson — Newline-Delimited JSON for Streaming
Learn about NDJSON (Newline-Delimited JSON) and its MIME type application/x-ndjson for streaming, log processing, and bulk API operations.
Detailed Explanation
What is NDJSON?
NDJSON (Newline-Delimited JSON), also known as JSON Lines, is a format where each line contains a valid JSON value, separated by newline characters (\n).
MIME Type
application/x-ndjson
Common file extensions: .ndjson, .jsonl
Example
{"id": 1, "name": "Alice", "role": "admin"}
{"id": 2, "name": "Bob", "role": "user"}
{"id": 3, "name": "Carol", "role": "user"}
Why Not Standard JSON?
Standard application/json requires the entire document to be a single valid JSON structure (typically an array). This means:
- You must wait for the complete response before parsing
- Large datasets require buffering the entire body in memory
NDJSON solves both problems by allowing line-by-line streaming:
- Parse each line independently as it arrives
- No need to buffer the entire payload
- Process millions of records with constant memory
Streaming API Example
GET /api/events/stream HTTP/1.1
Accept: application/x-ndjson
HTTP/1.1 200 OK
Content-Type: application/x-ndjson
Transfer-Encoding: chunked
{"event": "user.created", "timestamp": 1700000000}
{"event": "order.placed", "timestamp": 1700000001}
Common Use Cases
- Elasticsearch Bulk API — uses NDJSON for bulk indexing
- Log aggregation — structured logs in NDJSON format
- Data pipelines — streaming data between microservices
- LLM APIs — streaming token-by-token responses
Use Case
Use application/x-ndjson when building streaming APIs, processing large datasets line by line, or integrating with systems like Elasticsearch that use NDJSON for bulk operations. It is the natural choice for real-time event streams and log processing pipelines.