The project is in a healthy, maintained state
Apply and generate JSON patches using RFC 7396 Merge Patch and RFC 6902 JSON Patch. Supports add, remove, replace, move, copy, and test operations.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

philiprehberger-json_merge

Tests Gem Version Last updated

JSON Merge Patch (RFC 7396) and JSON Patch (RFC 6902) for Ruby

Requirements

  • Ruby >= 3.1

Installation

Add to your Gemfile:

gem "philiprehberger-json_merge"

Or install directly:

gem install philiprehberger-json_merge

Usage

require "philiprehberger/json_merge"

RFC 7396 Merge Patch

target = { "a" => 1, "b" => 2 }
patch  = { "b" => 3, "c" => 4 }

Philiprehberger::JsonMerge.merge_patch(target, patch)
# => {"a"=>1, "b"=>3, "c"=>4}

Remove keys with nil:

Philiprehberger::JsonMerge.merge_patch({ "a" => 1, "b" => 2 }, { "b" => nil })
# => {"a"=>1}

RFC 6902 JSON Patch

doc = { "name" => "Alice", "age" => 30 }
ops = [
  { "op" => "replace", "path" => "/name", "value" => "Bob" },
  { "op" => "add", "path" => "/active", "value" => true },
  { "op" => "remove", "path" => "/age" }
]

Philiprehberger::JsonMerge.apply(doc, ops)
# => {"name"=>"Bob", "active"=>true}

Generate Patches

RFC 6902 diff:

source = { "a" => 1, "b" => 2 }
target = { "a" => 1, "b" => 3, "c" => 4 }

Philiprehberger::JsonMerge.diff(source, target)
# => [{"op"=>"replace", "path"=>"/b", "value"=>3}, {"op"=>"add", "path"=>"/c", "value"=>4}]

RFC 7396 merge diff:

Philiprehberger::JsonMerge.merge_diff(source, target)
# => {"b"=>3, "c"=>4}

API

Method Description
JsonMerge.merge_patch(target, patch) Apply RFC 7396 merge patch
JsonMerge.apply(target, operations) Apply RFC 6902 JSON Patch
JsonMerge.diff(source, target) Generate RFC 6902 patch operations
JsonMerge.merge_diff(source, target) Generate RFC 7396 merge patch

Development

bundle install
bundle exec rspec      # Run tests
bundle exec rubocop    # Check code style

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT