Unfortunately dnf does not have machine-readable output (might change eventually Implement machine-readable output for commands · Issue #867 · rpm-software-management/dnf5 · GitHub, but I guess that will only be useful in the future…). But a dnf module could bundle a small Python script similar to GitHub - LINBIT/dnfjson: A wrapper around libdnf that produces JSON output that can be run as a subprocess with the correct Python interpreter to handle actual operations.