An implementation of JSON Pointer (RFC 6901) for Elixir.
Add a dependency to your project mix.exs
:
def deps do
[
{:odgn_json_pointer, "~> 3.1.0"}
]
end
iex> JSONPointer.get( %{ "fridge" => %{ "door" => "milk" } }, "/fridge/door" )
{:ok, "milk"}
iex> JSONPointer.set( %{}, "/example/msg", "hello")
{:ok, %{ "example" => %{ "msg" => "hello" }}, nil }
iex> JSONPointer.add( %{ "fridge" => [ "milk", "cheese" ]}, "/fridge/1", "salad")
{:ok, %{ "fridge" => [ "milk", "salad", "cheese" ]}, [ "milk", "cheese" ] }
iex> JSONPointer.has?( %{ "milk" => true, "butter" => false}, "/butter" )
true
iex> JSONPointer.test( %{ "milk" => "skimmed", "butter" => false}, "/milk", "skimmed" )
{:ok, %{ "milk" => "skimmed", "butter" => false} }
iex> JSONPointer.remove( %{"fridge" => %{ "milk" => true, "butter" => true}}, "/fridge/butter" )
{:ok, %{"fridge" => %{"milk"=>true}}, true }
iex> JSONPointer.dehydrate( %{"a"=>%{"b"=>["c","d"]}} )
{:ok, [{"/a/b/0", "c"}, {"/a/b/1", "d"}] }
iex> iex> JSONPointer.hydrate( [ {"/a/1/b", "single"} ] )
{:ok, %{"a" => %{"1" => %{"b" => "single"}}}}
iex> JSONPointer.merge( %{"a"=>1}, %{"b"=>2} )
{:ok, %{"a"=>1,"b"=>2} }
iex> JSONPointer.transform( %{ "a"=>4, "b"=>%{ "c" => true }}, [ {"/b/c", "/valid"}, {"/a","/count", fn val -> val*2 end} ] )
{:ok, %{"count" => 8, "valid" => true}}
Full documentation can be found at https://hexdocs.pm/odgn_json_pointer.
Inspiration from https://github.com/manuelstofer/json-pointer
Made without peeking (much) at the source of https://github.com/xavier/json_pointer
Made in Exeter, UK.
Copyright (c) 2024 Alexander Veenendaal
This work is free. You can redistribute it and/or modify it under the terms of the MIT License. See the LICENSE.md file for more details.