Sub topic 11
JSON output and CycloneDX SBOMs
This chapter covers Trivy's machine-readable outputs: a JSON scan report and a CycloneDX SBOM. It assumes you can run trivy image and that you know what an SBOM is from the earlier chapter. It also assumes you can read basic JSON.
Why machine-readable output
The default table is made for people. Other tools can't read it reliably. JSON gives dashboards, ticketing scripts and policy checks a stable structure to parse. An SBOM answers a different question. A scan report says which findings exist today. An SBOM lists which components are inside the image, so you can re-check it when new CVEs appear.
| Output | Contains | Typical use |
|---|---|---|
| Table (default) | Findings, formatted for reading | Local review |
| JSON | Findings plus metadata | Scripts, dashboards, ticket creation |
| CycloneDX | Component inventory | Storage, audits, later re-scanning |
Writing a JSON report
Use --format to pick the format and --output to write to a file instead of the terminal.
trivy image --format json --output result.json python:3.12-slimThe report has a top-level Results array. Each entry is one target, such as the operating system packages or a language lockfile. Each target holds a Vulnerabilities list. A target with no findings may omit that key.
Trivy's JSON output is a documented schema, but field names can change between major versions. Pin the Trivy version in automation.
Reading the report with jq
jq is a command-line JSON processor. This command lists each finding as one line.
jq -r '.Results[]?.Vulnerabilities[]? | [.VulnerabilityID, .Severity, .PkgName, .InstalledVersion, (.FixedVersion // "none")] | @tsv' result.jsonThe ? after Results[] and Vulnerabilities[] stops jq from failing when a key is missing. The // "none" supplies a value when FixedVersion is absent, which means no fix is available yet.
To count findings by severity:
jq -r '[.Results[]?.Vulnerabilities[]?.Severity] | group_by(.) | map({(.[0]): length}) | add' result.jsonGenerating a CycloneDX SBOM
CycloneDX is an SBOM standard. Trivy can write it directly.
trivy image --format cyclonedx --output sbom.cdx.json python:3.12-slimThe file lists components, such as OS packages and language libraries, with names, versions and package URLs (purl). A purl is a standard identifier like pkg:deb/debian/[email protected]. The same purl can be matched against vulnerability databases by different tools.
You can scan an SBOM later without pulling the image again:
trivy sbom sbom.cdx.jsonThis is useful for archived releases. The SBOM stays fixed, but the vulnerability database keeps updating, so the same file can produce new findings next month.
Common errors
- Redirecting the table to a file. Without
--format json, a file namedresult.jsonstill holds a table, andjqfails to parse it. - Mixing log lines into output. Use
--outputrather than shell redirection, so progress messages on standard error cannot end up in the file. - Assuming every target has findings. Handle missing
Vulnerabilitieskeys, as the?operators do above. - Treating the SBOM as a scan result. It has no severity or fix data.
Exercise
You saved an SBOM for release 1.4 six months ago. A new CVE affects a library in it. How can you check whether release 1.4 is affected without pulling the image, and why might the result differ from the scan you ran at release time?
Show answer
Run trivy sbom sbom.cdx.json. Trivy matches the stored component list against its current vulnerability database. The result can differ because the database now contains CVEs published after the original scan, while the component list is unchanged.
Exercise
A script runs jq '.Results[].Vulnerabilities[]' result.json and fails on some images with an error about iterating over null. What is the cause and the fix?
Show answer
Some entries in Results have no Vulnerabilities key, so jq iterates over null. Add the ? operator: .Results[]?.Vulnerabilities[]?.