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 field 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: cjd [-h] -i INPUT INPUT [-o OUTPUT] [-b] (-c CONFIG | -x EXCLUDE [EXCLUDE ...] | -p {cdxgen,cdxgen-extended})
options:
-h, --help show this help message and exit
-i INPUT INPUT, --input INPUT INPUT
Two JSON files to compare.
-o OUTPUT, --output OUTPUT
Export JSON of differences to this file.
-a, --allow-new-versions
Allow new versions in BOM comparison.
-b, --bom-diff Produce a comparison of CycloneDX BOMs.
-c CONFIG, --config-file CONFIG
Import TOML configuration file.
-x EXCLUDE [EXCLUDE ...], --exclude EXCLUDE [EXCLUDE ...]
Exclude field(s) from comparison.
-p {cdxgen,cdxgen-extended}, --preset {cdxgen,cdxgen-extended}
Preset to use
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 = ["serialNumber", "metadata.timestamp"]
sort_keys = ["url", "content", "ref", "name", "value"]
[bom_diff]
allow_new_versions = false
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file custom_json_diff-0.5.0.tar.gz.
File metadata
- Download URL: custom_json_diff-0.5.0.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/5.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a699c5b58d50d668a5c16a10491c718e2ad220251cc53698e272648cf43e81b7
|
|
| MD5 |
214b88872eba99778512176a3f1b7917
|
|
| BLAKE2b-256 |
2a78e05bbaf25d465ae4545a95180755e437f42c320ec20253ef78da075331fb
|
File details
Details for the file custom_json_diff-0.5.0-py3-none-any.whl.
File metadata
- Download URL: custom_json_diff-0.5.0-py3-none-any.whl
- Upload date:
- Size: 12.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/5.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d9a57c80940d6c00f891556178afba7a54c0fd7b28ea00f6a20a88634426b0fc
|
|
| MD5 |
42eccd892f54de23e58076806f9f7662
|
|
| BLAKE2b-256 |
9b045f850b6b42d723675c6ca48462c65e1b9d02565616dc61fcd4c1acb9fb76
|