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 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] (-c CONFIG | -x EXCLUDE [EXCLUDE ...] | -p {cdxgen})

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
  -c CONFIG, --config-file CONFIG
                        Import TOML configuration file
  -x EXCLUDE [EXCLUDE ...], --exclude EXCLUDE [EXCLUDE ...]
                        Exclude field(s) from comparison
  -p {cdxgen}, --preset {cdxgen}
                        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. custom-json-diff will create a regex which will account for the array index in the field name. 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"]

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-0.4.0.tar.gz (58.7 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-0.4.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: custom_json_diff-0.4.0.tar.gz
  • Upload date:
  • Size: 58.7 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-0.4.0.tar.gz
Algorithm Hash digest
SHA256 04130c57412150fad23efb0f44c5df7428f4f477cf3177dab33993063c69bcf2
MD5 4f57a783594193e8dd452bdc1cc75b0b
BLAKE2b-256 7aad89a857d669237ab7979fd45fe1c6243c3ef9f6fbcdbe54cdd3cdd651d2f6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for custom_json_diff-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4f48a8c270b40fe2a46c370857eed5e972ec22ed7e6b90c531e9ee578ad7d6f9
MD5 b8fc4ddd7ecd9f29f7ee262b3afbec98
BLAKE2b-256 a2644eb3e1970ef60ebcc2b8fa42e300eb2505d0ae474f0fe562167da20b8903

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

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

This release

0.4.0 This release

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