json_patch

Crystal shard for JSON Patch (RFC 6902), JSON Pointer (RFC 6901), and Merge Patch (RFC 7396) with diff generation.

json_patch

A Crystal shard for working with JSON patches and diffs. It implements four specifications on top of the stdlib JSON::Any type, with no external dependencies:

  • RFC 6902 — JSON Patch: apply a sequence of operations (add, remove, replace, move, copy, test) to a JSON document.
  • RFC 6901 — JSON Pointer: address any value inside a JSON document with a slash-delimited path string (e.g. /foo/0/bar).
  • RFC 7396 — JSON Merge Patch: apply a partial object to merge changes into a document, where null values delete keys.
  • Diff generation: given two JSON::Any values, produce the minimal RFC 6902 patch that transforms one into the other.

All operations are functional — apply and merge return new JSON::Any values and never mutate their inputs. You can call most things as methods on JSON::Any directly (.at, .diff, .merge, .apply), or via the JSONPatch module (JSONPatch.parse, JSONPatch.merge, JSONPatch.diff).

Installation

Add to shard.yml:

dependencies:
  json_patch:
    github: naqvis/json_patch

Then shards install and require "json_patch".

Usage

require "json_patch"

# --- RFC 6902 JSON Patch ---
patch = JSONPatch.parse(%([{"op": "replace", "path": "/name", "value": "Jane"}]))
patched = patch.apply_to(doc)
patched = doc.apply(patch)            # equivalent

# --- Diff generation ---
patch = before.diff(after)           # => JSONPatch::Patch
patch.apply_to(before)               # == after

# --- RFC 6901 JSON Pointer ---
doc.at("/foo/0")                     # => JSON::Any, raises if missing
doc.at?("/foo/0")                    # => JSON::Any?
doc.has?("/foo/0")                   # => Bool

# --- RFC 7396 Merge Patch ---
target.merge(patch_json)             # => new JSON::Any
JSONPatch.merge_patch(before, after) # produce a merge patch from a diff
JSONPatch.compose(a, b)              # combine two merge patches into one

Conformance

The official json-patch-tests suite is vendored under spec/support and run as part of crystal spec.

License

MIT. See LICENSE.

Contributing

  1. Fork it (https://github.com/naqvis/json_patch/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

Repository

json_patch

Owner
Statistic
  • 0
  • 0
  • 0
  • 0
  • 0
  • about 3 hours ago
  • July 8, 2026
License

MIT License

Links
Synced at

Wed, 08 Jul 2026 16:11:26 GMT

Languages