JSON Output

The gcovr command can also generate a JSON output using the --json and --json-pretty options:

gcovr --json coverage.json

The --json-pretty option generates an indented JSON output that is easier to read.

If you just need a summary of the coverage information, similar to the tabulated text based output, you can use --json-summary instead (see JSON Summary Output).

Multiple JSON files can be merged into the coverage data with sum of lines and branches execution, see Merging Coverage Data.

JSON Format Reference

Structure of file is based on gcov JSON intermediate format with additional key names specific to gcovr.

Structure of the JSON is following:

{
    "gcovr/format_version": gcovr_json_version
    "files": [file]
}

gcovr_json_version: version of gcovr JSON format. This is independently versioned from gcovr itself.

Each file has the following form:

{
    "file": file
    "lines": [line]
}

file: path to source code file, relative to gcovr root directory.

Each line has the following form:

{
    "branches": [branch]
    "count": count
    "line_number": line_number
    "gcovr/noncode": gcovr_noncode
}

gcovr_noncode: if True coverage info on this line should be ignored

Each branch has the following form:

{
  "count": count
  "fallthrough": fallthrough
  "throw": throw
}

file, line and branch have the structure defined in gcov intermediate format. This format is documented at https://gcc.gnu.org/onlinedocs/gcc/Invoking-Gcov.html#Invoking-Gcov.

JSON Summary Output

The --json-summary option output coverage summary in a machine-readable format for additional post processing. The format is identical to JSON output --json option without detailed lines information. The --json-summary-pretty option generates an indented JSON summary output that is easier to read. Consider the following command:

gcovr --json-summary-pretty --json-summary

This generates an indented JSON summary:

{
    "branch_covered": 1,
    "branch_percent": 50.0,
    "branch_total": 2,
    "files": [
        {
            "branch_covered": 1,
            "branch_percent": 50.0,
            "branch_total": 2,
            "filename": "example.cpp",
            "function_covered": 2,
            "function_percent": 100.0,
            "function_total": 2,
            "line_covered": 6,
            "line_percent": 85.7,
            "line_total": 7
        }
    ],
    "function_covered": 2,
    "function_percent": 100.0,
    "function_total": 2,
    "gcovr/summary_format_version": "0.5",
    "line_covered": 6,
    "line_percent": 85.7,
    "line_total": 7,
    "root": "."
}

New in version 5.0: Added --json-summary and --json-summary-pretty.