Skip to main content

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 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

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

positional arguments:
  {bom-diff}            subcommand help
    bom-diff            compare CycloneDX BOMs

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 [EXCLUDE ...]
                        Exclude field(s) from comparison.

bom-diff usage

usage: cjd bom-diff [-h] [--allow-new-versions] [--allow-new-data] [--components-only] [-r REPORT_TEMPLATE] [--include-extra INCLUDE [INCLUDE ...]]

options:
  -h, --help            show this help message and exit
  --allow-new-versions, -anv
                        Allow newer versions in second BOM to pass.
  --allow-new-data, -and
                        Allow populated BOM values in newer BOM to pass against empty values in original BOM.
  --components-only     Only compare components.
  -r REPORT_TEMPLATE, --report-template REPORT_TEMPLATE
                        Jinja2 template to use for report generation.
  --include-extra INCLUDE [INCLUDE ...]
                        Include properties/evidence/licenses/hashes in comparison (list which with space inbetween).

Bom Diff

The bom-diff 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 bom-diff --include-extra and whichever field(s) you wish to include :

  • properties
  • evidence
  • licenses
  • hashes
  • externalReferences

You can use the -x --exclude switch before the bom-diff command to exclude any of these (see Specifying fields to exclude ).

Default included fields:

components:

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

services

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

dependencies

  • ref
  • dependsOn

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.

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. This option only applies to the fields included by default.

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 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"]

[bom_settings]
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-1.1.0.tar.gz (19.6 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-1.1.0-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

Details for the file custom_json_diff-1.1.0.tar.gz.

File metadata

  • Download URL: custom_json_diff-1.1.0.tar.gz
  • Upload date:
  • Size: 19.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for custom_json_diff-1.1.0.tar.gz
Algorithm Hash digest
SHA256 7965842750e256fcb1dab138b625c9a9c3f00e5c5a5bc58ed7b9bd74e0be6fdc
MD5 14d350894a5d11352acc0b7110ac3ca2
BLAKE2b-256 5b9a8c9291bda9910d3c364f863084ffbf5a3a243ed5502d44150414a0810072

See more details on using hashes here.

File details

Details for the file custom_json_diff-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for custom_json_diff-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5e19d450fc311d6d48f0c910f181ef8fcc1a27c8418a16f6e49fd372b605221f
MD5 a56401001d741e422d393876cbe92141
BLAKE2b-256 ed171e78fbfc418d92108f55e9f0e4798305140c141c871fca93a40074f91508

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

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

This release

1.1.0 This release

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