JsonMergePatch

CIHex.pmHexdocs.pm

RFC 7396 JSON Merge Patch for Elixir.

Applies a merge patch to decoded JSON values: maps with string keys, lists, strings, numbers, booleans, and nil. Decode with JSON or Jason, then call JsonMergePatch.apply_patch/2.

JsonMergePatch.apply_patch(
%{"a" => "b", "c" => %{"d" => "e", "f" => "g"}},
%{"a" => "z", "c" => %{"f" => nil}}
)
# => {:ok, %{"a" => "z", "c" => %{"d" => "e"}}}

Installation

def deps do
[
{:json_merge_patch, "~> 0.1.0"}
]
end

Usage

JsonMergePatch.apply_patch/2 returns {:ok, json} or {:error, exception}. JsonMergePatch.apply_patch!/2 raises JsonMergePatch.Error.

{:ok, target} = JSON.decode(document)
{:ok, patch} = JSON.decode(body)
JsonMergePatch.apply_patch(target, patch)
JsonMergePatch.apply_patch(target, patch, max_depth: 32)
JsonMergePatch.apply_patch!(target, patch)

Authorize the change in the caller, then apply (RFC 7396 §5).

Sponsor

Netoum

Contributing

See CONTRIBUTING.md. This project follows the Code of Conduct.

License

MIT © Netoum. See LICENSE.