@@ -602,7 +602,30 @@ homogeneous across Unix dialects. Thus, if you write a script
602602to post-process field output for AIX, it probably will work for
603603HP-UX, Solaris, and Ultrix as well.
604604
605- Support for other formats e.g. JSON is planned.
605+ ### JSON Output
606+
607+ Lsof supports two JSON output modes:
608+
609+ - ** ` -J ` ** (nested JSON) — produces a single JSON object containing a
610+ ` processes ` array, where each process has a ` files ` array of open-file
611+ entries. Suitable for tools that consume a complete document (e.g.
612+ ` python3 -m json.tool ` , ` jq ` ).
613+
614+ - ** ` -j ` ** (JSON Lines) — produces one JSON object per line, combining
615+ process and file fields in a single denormalized record. Suitable for
616+ streaming pipelines, log ingestion (Splunk, Datadog, Elastic Stack),
617+ and line-oriented tools.
618+
619+ Both modes reuse the ` -F ` field-selection mechanism. For example,
620+ ` lsof -J -Fpcfn ` limits output to PID, command, fd, and name fields.
621+
622+ ** Encoding caveat:** JSON (RFC 8259) requires strings to be valid UTF-8,
623+ but Unix file names are arbitrary byte sequences. When file names
624+ contain non-UTF-8 bytes, lsof passes them through unchanged — the output
625+ is technically not valid JSON, but preserves the original file name.
626+ This is the same approach taken by ` lsfd ` , ` ip -j ` , and most Linux tools
627+ that produce JSON. If your consumer requires strict UTF-8, use a filter
628+ such as ` iconv ` or Python's ` surrogateescape ` codec error handler.
606629
607630## The Lsof Exit Code and Shell Scripts
608631
0 commit comments