contentviews: add support for displaying NDJSON (#5608) by johnnylemonny · Pull Request #8479 · mitmproxy/mitmproxy · GitHub
Skip to content

contentviews: add support for displaying NDJSON (#5608) - #8479

Open
johnnylemonny wants to merge 2 commits into
mitmproxy:mainfrom
johnnylemonny:feature/ndjson-contentview
Open

johnnylemonny wants to merge 2 commits into
mitmproxy:mainfrom
johnnylemonny:feature/ndjson-contentview

Conversation

@johnnylemonny

Copy link
Copy Markdown

Hi team! 👋 My first contribution to this project!

This PR picks up the feature discussed in old #5608 to support Newline-Delimited JSON (NDJSON) in the existing JSON content view, following the approach suggested by @Prinzhorn and @mhils.

When inspecting Elasticsearch _bulk payloads, LLM/streaming responses, or log streams formatted as NDJSON, mitmproxy previously fell back to raw/text view because json.loads failed on multiple concatenated JSON records.

Changes

  • Graceful NDJSON fallback: In JSONContentview.prettify, if parsing the whole payload as a single JSON document fails, we now attempt to parse each non-empty line as a distinct JSON object. Each record is formatted with 4-space indentation and joined with newlines.
  • MIME type detection: Added application/x-ndjson and application/ndjson (as well as *ndjson subtypes) to render_priority so NDJSON streams are recognized automatically.
  • Error handling: If the data contains invalid JSON on any line or is completely empty/whitespace, it raises ValueError as before, preserving existing error semantics.
  • Changelog: Added a bullet point under ## Unreleased: mitmproxy next.

Checklist

  • I have updated tests where applicable (test/mitmproxy/contentviews/test__view_json.py).
  • I have added an entry to the CHANGELOG.
  • Tests pass locally (uv run pytest test/mitmproxy/contentviews/test__view_json.py - 100% coverage on _view_json.py).
  • Linting and typing pass cleanly (uv run ruff check . and uv run mypy).

Thanks for reviewing, and thank you for maintaining mitmproxy!

@johnnylemonny
johnnylemonny force-pushed the feature/ndjson-contentview branch from 716f3a7 to 53fc6c2 Compare October 21, 2026 18:38

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant