Skip to main content

Object Diff

Object diff (odiff) is a little tool/library for diffing objects - it's a little specific in a couple of places to a thing I'm doing at work but it might be useful elsewhere.

Essentially, you feed it two files, JSON or YAML, to compare and discrepancies are listed in the console.

Usage

usage: odiff [-h] [--log-level LOG_LEVEL] [--output-type OUTPUT_TYPE]
             [--list-cfg LIST_CFG]
             [files ...]

positional arguments:
  files                 two files to diff

options:
  -h, --help            show this help message and exit
  --log-level LOG_LEVEL
                        log level name
  --output-type OUTPUT_TYPE, --output OUTPUT_TYPE, -o OUTPUT_TYPE
                        report output flavour
  --config CONFIG, -c CONFIG
                        yaml config file

Example Output

odiff -c aux/eg-cfg.yaml aux/eg1.json aux/eg2.json
[WARN:odiff] Discrepancy found for path '.alpha' but was excluded
╭──────────────┬────────────┬────────────────┬───────────────────────╮
│ Variant      │ Path       │ Lvalue         │ Rvalue                │
├──────────────┼────────────┼────────────────┼───────────────────────┤
│ modification │ .beta      │ world          │ , world               │
├──────────────┼────────────┼────────────────┼───────────────────────┤
│ subtraction  │ .gamma[]   │ None           │ [                     │
│              │            │                │   "isn't",            │
│              │            │                │   "not-in-eg1"        │
│              │            │                │ ]                     │
├──────────────┼────────────┼────────────────┼───────────────────────┤
│ addition     │ .gamma[]   │ [              │ None                  │
│              │            │   "is",        │                       │
│              │            │   "not-in-eg2" │                       │
│              │            │ ]              │                       │
├──────────────┼────────────┼────────────────┼───────────────────────┤
│ subtraction  │ .delta[    │ None           │ {                     │
│              │   Ct2fhriU │                │   "_id": "Ct2fhriU",  │
│              │ ]          │                │   "key1": "i'm added" │
│              │            │                │ }                     │
├──────────────┼────────────┼────────────────┼───────────────────────┤
│ modification │ .delta[    │ value0         │ i've been modified    │
│              │   vCjpIL2A │                │                       │
│              │ ].key0     │                │                       │
╰──────────────┴────────────┴────────────────┴───────────────────────╯

Instead of using a configuration file (see Configuration) you can also provide the same configuration directly in the command:

odiff aux/eg1.json aux/eg2.json --li '.delta: _id' --exc '.alpha'

Configuration

List Index Configuration

You may have spotted in the Example Output that we pass the -c option (--list-config) and that the output made use of this key in the children of .delta to "align" the list elements together - this allows us to diff matching objects despite them not necessarily being the correct order.

The format of this is a simple string-string, key-value pairing in YAML, e.g.:

.delta: _id

You can see it takes a JQ-ish form for the object pathing. So, if your input is a list of object, you should provide an index for the . key.

Exclusions

Another thing you may have spotted in the output is the log line at the top about the excluded discrepancy on .alpha.

This is what the .exclusions list is for, anything you wish to ignore in either the left or right file can be listed here.

Contributing

This repo uses Pre-commit for some sanity checks, so:

pre-commit install

There are literally zero tests aside the examples...

Release files for odiff 0.1.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for odiff 0.1.8
File Size Uploaded
odiff-0.1.8.tar.gz 9.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for odiff 0.1.8
File Interpreter ABI Platform
odiff-0.1.8-py3-none-any.whl Python 3 none any Details

Total release size: 20.8 kB

Release files / odiff-0.1.8.tar.gz

Download URL odiff-0.1.8.tar.gz
Size 9.9 kB
Tags Source
SHA-256 checksum
How to use checksums
d91ce142ff8e528a434ddb10c26fa6b40d9580461b6fedbb6cf7951bd25f8705
BLAKE2b-256 checksum
How to use checksums
510e99370658628a1442357de0199ab1b56a0999915aa6149fb4d2931f2ebe53
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.11.9 Linux/6.5.0-1022-azure

Release files / odiff-0.1.8-py3-none-any.whl

Download URL odiff-0.1.8-py3-none-any.whl
Size 10.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1539db6d0ff6523a41bbd812d17f0a47df1a52323f6dfee54552fd4f28f08525
BLAKE2b-256 checksum
How to use checksums
9a8ae57e52575ece90e46a9260a91de2c4cb1056c6ebb703069ec2df619063f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.11.9 Linux/6.5.0-1022-azure

Release history Release notifications | RSS feed

This release

0.1.8 This release

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

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