Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

custom-json-diff

Comparing two JSON files presents an issue when the two files have certain fields which are dynamically generated (e.g. timestamps), variable ordering, or other fields which need to be excluded or undergo specialized comparison for one reason or another. Enter custom-json-diff, which allows you to specify fields to ignore in the comparison and sorts all fields.

Installation

pip install custom-json-diff

CLI Usage

Note, you may use cjd rather than custom-json-diff to run.

usage: custom-json-diff [-h] [-v] -i INPUT INPUT [-o OUTPUT] [-c CONFIG] [-x EXCLUDE] [--debug] {preset-diff} ...

positional arguments:
  {preset-diff}         subcommand help
    preset-diff         Compare CycloneDX BOMs or Oasis CSAFs

options:
  -h, --help            show this help message and exit
  -v, --version         show program's version number and exit
  -i INPUT INPUT, --input INPUT INPUT
                        Two JSON files to compare - older file first.
  -o OUTPUT, --output OUTPUT
                        Export JSON of differences to this file.
  -c CONFIG, --config-file CONFIG
                        Import TOML configuration file (overrides commandline options).
  -x EXCLUDE, --exclude EXCLUDE
                        Exclude field(s) from comparison.
  --debug               Print debug messages.

preset-diff usage

usage: custom-json-diff preset-diff [-h] [--allow-new-versions] [--allow-new-data] [--type PRESET_TYPE] [-r REPORT_TEMPLATE] [--include-extra INCLUDE]

options:
  -h, --help            show this help message and exit
  --allow-new-versions, -anv
                        BOM only - allow newer versions in second BOM to pass.
  --allow-new-data, -and
                        Allow populated values in newer BOM or CSAF to pass against empty values in original BOM/CSAF.
  --type PRESET_TYPE    Either bom or csaf
  -r REPORT_TEMPLATE, --report-template REPORT_TEMPLATE
                        Jinja2 template to use for report generation.
  --include-extra INCLUDE
                        BOM only - include properties/evidence/licenses/hashes/externalReferences (list which with comma, no space, inbetween).

Preset Diffs

CJD offers advanced diffing for Cyclonedx BOM (v1.5 or v1.6) produced by CycloneDx Cdxgen and Oasis CSAF v2 produced by OWASP-dep-scan.

Bom Diff

The preset-diff --type bom command compares CycloneDx BOM components, services, and dependencies, as well as data outside of these parts.

Some fields are excluded from the component comparison by default but can be explicitly specified for inclusion using preset-diff --include-extra and whichever field(s) you wish to include (e.g. --include-extra properties,evidence,licenses:

  • properties
  • evidence
  • licenses
  • hashes
  • externalReferences

You can use the -x --exclude switch before the preset-diff command to exclude any of these (see Specifying fields to exclude) except for bom-ref, as that is needed for the comparison - if the bom-ref includes a version, that part can be excluded as needed (see Allowing newer versions).

Default included fields:

bomFormat metadata specVersion version

components:

  • author
  • bom-ref
  • description
  • group
  • name
  • publisher
  • purl
  • scope
  • type
  • version

services

  • name
  • endpoints
  • authenticated
  • x-trust-boundary

dependencies

  • dependsOn
  • ref

vulnerabilities

  • advisories
  • affects
  • analysis
  • bom-ref
  • cwes
  • description
  • detail
  • id
  • properties
  • published
  • ratings
  • recommendation
  • references
  • source
  • updated

CSAF Diff

CSAF diffing includes the following fields at this time. Only the vulnerabilities section uses the allows new data option. Fields can be excluded using the -x --exclude as described for bom diffing except for title as that is currently being populated by depscan with the bom-ref of the vulnerability as a unique id.

document

product_tree

vulnerabilities

  • acknowledgements
  • cwe
  • cve
  • discovery_date
  • ids
  • notes
  • product_status
  • references
  • scores
  • title

Allowing newer versions

[Currently BOM only] The --allow-new-versions option attempts to parse component versions and ascertain if a discrepancy is attributable to an updated version. Dependency refs and dependents are compared with the version string removed rather than checking for a newer version.

Allowing new data

The --allow-new-data option allows for empty fields in the original BOM not to be reported as a difference when the data is populated in the second specified BOM. It also addresses when a field such as properties is expanded, checking that all original elements are still present but allowing additional elements in the newer BOM.

The --components-only option only analyzes components, not services, dependencies, or other data.

Specifying fields to exclude

To exclude fields from comparison, use the -x or --exclude flag and specify the field name(s) to exclude. The json will be flattened, so fields are specified using dot notation. For example:

{
    "field1": {
        "field2": "value", 
        "field3": [
            {"a": "val1", "b": "val2"}, 
            {"a": "val3", "b": "val4"}
        ]
    }
}

is flattened to:

{
    "field1.field2": "value",
    "field1.field3.[0].a": "val1",
    "field1.field3.[0].b": "val2",
    "field1.field3.[1].a": "val3",
    "field1.field3.[1].b": "val4"
}

To exclude field2, you would specify field1.field2. To exclude the a field in the array of objects, you would specify field1.field3.[].a (do NOT include the array index, just do []). Multiple fields may be specified separated by a comma (no space). To better understand what your fields should be, check out json-flatten, which is the package used for this function.

Sorting

custom-json-diff will sort the imported JSON alphabetically. If your JSON document contains arrays of objects, you will need to specify any keys you want to sort by in a toml file or use a preset. The first key located from the provided keys that is present in the object will be used for sorting.

TOML config file example

[settings]
excluded_fields = []
sort_keys = ["url", "content", "ref", "name", "value"]

[preset_settings]
type = "bom"
allow_new_data = false
allow_new_versions = true
components_only = false
include_extra = ["licenses", "properties", "hashes", "evidence", "externalReferences"]
report_template = "custom_json_diff/bom_diff_template.j2"

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

custom_json_diff-2.0.0b1.tar.gz (31.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

custom_json_diff-2.0.0b1-py3-none-any.whl (27.3 kB view details)

Uploaded Python 3

File details

Details for the file custom_json_diff-2.0.0b1.tar.gz.

File metadata

  • Download URL: custom_json_diff-2.0.0b1.tar.gz
  • Upload date:
  • Size: 31.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for custom_json_diff-2.0.0b1.tar.gz
Algorithm Hash digest
SHA256 95427179392a15008a6099607cdc209b2d7c5c3302f35d29923682030af9d4dc
MD5 eeae78e7b3b399cccdb94b7146aa0161
BLAKE2b-256 43c536eb8663de36284bc808879006ca9f5ded2db7a06c649fdc415eb6aa046c

See more details on using hashes here.

File details

Details for the file custom_json_diff-2.0.0b1-py3-none-any.whl.

File metadata

File hashes

Hashes for custom_json_diff-2.0.0b1-py3-none-any.whl
Algorithm Hash digest
SHA256 e80ae9ce68f79fa5c91ce2ad7be671ce403d5e12bfc0059b17361a31063f2d2c
MD5 7b4ccdd981b2f9f158ae2e3137ce6432
BLAKE2b-256 5dd2179249315f25a482e2d990cd24f41127a7c0baa11bdb56931dc362bfb1bc

See more details on using hashes here.

Release history Release notifications | RSS feed

2.1.6

2 files

2.1.5

2 files

2.1.4

2 files

2.1.3

2 files

2.1.2

2 files

2.1.1

2 files

2.1.0

2 files

2.0.0

2 files

This release

2.0.0b1 This release

2 files

1.7.0

2 files

1.6.0

2 files

1.5.6

2 files

1.5.5

2 files

1.5.4

2 files

1.5.3

2 files

1.5.2

2 files

1.5.1

2 files

1.5.0

2 files

1.4.0

2 files

1.3.0

2 files

1.2.1

2 files

1.2.0

2 files

1.1.0

2 files

1.0.0

2 files

0.7.0

2 files

0.6.0

2 files

0.5.0

2 files

0.4.0

2 files

0.3.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page