https://github.com/corka149/jsonpatch
A implementation of RFC 6902 in pure Elixir.
https://github.com/corka149/jsonpatch
elixir jsonpatch
Last synced: 10 months ago
JSON representation
A implementation of RFC 6902 in pure Elixir.
- Host: GitHub
- URL: https://github.com/corka149/jsonpatch
- Owner: corka149
- License: mit
- Created: 2020-02-16T19:43:50.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2025-03-01T12:15:14.000Z (11 months ago)
- Last Synced: 2025-04-04T13:13:09.510Z (10 months ago)
- Topics: elixir, jsonpatch
- Language: Elixir
- Homepage:
- Size: 200 KB
- Stars: 46
- Watchers: 2
- Forks: 12
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Jsonpatch

[](https://coveralls.io/github/corka149/jsonpatch?branch=master)
[](https://shields.io/)
[](https://github.com/corka149/jsonpatch/graphs/commit-activity)
[](https://hex.pm/packages/jsonpatch)
An implementation of [RFC 6902](https://tools.ietf.org/html/rfc6902) in pure Elixir.
Features:
- Creating a patch by comparing to maps and lists
- Apply patches to maps and lists - supports operations:
- add
- replace
- remove
- copy
- move
- test
- Escaping of "`/`" (by "`~1`") and "`~`" (by "`~0`")
- Allow usage of `-` for appending things to list (Add and Copy operation)
## Getting started
### Installation
The package can be installed by adding `jsonpatch` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:jsonpatch, "~> 2.2"}
]
end
```
The docs can be found at [https://hexdocs.pm/jsonpatch](https://hexdocs.pm/jsonpatch).
### Create a diff
```elixir
iex> source = %{"name" => "Bob", "married" => false, "hobbies" => ["Sport", "Elixir", "Football"]}
iex> destination = %{"name" => "Bob", "married" => true, "hobbies" => ["Elixir!"], "age" => 33}
iex> Jsonpatch.diff(source, destination)
[
%{path: "/married", value: true, op: "replace"},
%{path: "/hobbies/2", op: "remove"},
%{path: "/hobbies/1", op: "remove"},
%{path: "/hobbies/0", value: "Elixir!", op: "replace"},
%{path: "/age", value: 33, op: "add"}
]
```
### Apply patches
```elixir
iex> patch = [
%{op: "add", path: "/age", value: 33},
%{op: "replace", path: "/hobbies/0", value: "Elixir!"},
%{op: "replace", path: "/married", value: true},
%{op: "remove", path: "/hobbies/1"},
%{op: "remove", path: "/hobbies/2"}
]
iex> target = %{"name" => "Bob", "married" => false, "hobbies" => ["Sport", "Elixir", "Football"]}
iex> Jsonpatch.apply_patch(patch, target)
{:ok, %{"name" => "Bob", "married" => true, "hobbies" => ["Elixir!"], "age" => 33}}
```
## Important sources
- [Official RFC 6902](https://tools.ietf.org/html/rfc6902)
- [Inspiration: python-json-patch](https://github.com/stefankoegl/python-json-patch)