JSON output makes Codex runs easy to integrate into automation, CI pipelines, and log processors without relying on brittle plain-text parsing.
The codex exec command can emit a newline-delimited JSONL event stream when the –json flag is enabled. Each line is a standalone JSON object that describes the run lifecycle, such as thread creation, turn boundaries, tool activity, token usage, and the assistant message content.
The JSON event stream is more verbose than default console output and can include sensitive material like prompts, tool outputs, and file excerpts. Redirect output into a dedicated log file and review it before sharing or committing.
Steps to enable JSON output in Codex exec:
- Run Codex with JSON output enabled.
$ codex exec --json --skip-git-repo-check "Return OK." {"type":"thread.started","thread_id":"019bd456-d3d4-70c3-90de-51d31a6c8571"} {"type":"turn.started"} {"type":"item.completed","item":{"id":"item_0","type":"reasoning","text":"**Confirming readiness for response**"}} {"type":"item.completed","item":{"id":"item_1","type":"agent_message","text":"OK."}} {"type":"turn.completed","usage":{"input_tokens":4547,"cached_input_tokens":2432,"output_tokens":8}}Use –skip-git-repo-check to bypass repository checks when running outside a Git working tree.
Event types include thread.started, turn.*, item.*, and error.
Use --experimental-json on older builds that have not promoted JSON output to --json.
- Save the JSONL event stream into a file for later parsing.
$ codex exec --json --skip-git-repo-check "Return OK." > codex.jsonl
The JSONL log can contain prompts, tool outputs, and file contents; avoid committing it to Git or sharing it without review.
- Count the captured JSON events to confirm output was written.
$ wc -l codex.jsonl 5 codex.jsonl
- Confirm the log file contains JSON objects on separate lines.
$ head -n 2 codex.jsonl {"type":"thread.started","thread_id":"019bd457-0bfc-7272-9f80-2c709bc6a6bb"} {"type":"turn.started"} - Extract the assistant message text from the JSONL stream with a JSON parser.
$ jq -r 'select(.type=="item.completed" and .item.type=="agent_message") | .item.text' codex.jsonl OK.
Event schemas can differ across Codex versions; adjust filters after checking a few lines with head.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
