a high level library for working with json in erlang (17.0+)
json is built via rebar, and continuous integration testing provided by travis-ci
json is released under the terms of the MIT license
copyright 2014 alisdair sullivan
erlang 17.0+ is required to use this library due to it's reliance on the new map implementation
jsx is used when converting between erlang binary() and json() types
jsonpointer is used for handling json paths in accordance with rfc6901.
$ rebar get-deps
$ rebar compile
$ rebar eunit
convert between json() and binary() erlang types.
ExampleJSON
will be used as an example JSON input for the rest of the quickstart.
1> ExampleJSON = json:from_binary(<<"{\"library\": \"json\", \"awesome\": true,
1> \"list\": [{\"a\": 1}, {\"b\": 2}, {\"c\": 3} ]}">>).
#{<<"awesome">> => true,
<<"library">> => <<"json">>,
<<"list">> => [#{<<"a">> => 1},#{<<"b">> => 2},#{<<"c">> => 3}]}
1> json:to_binary(ExampleJSON).
<<"{\"awesome\":true,\"library\":\"json\",\"list\":[{\"a\":1},{\"b\":2},{\"c\":3}]}">>
1> json:get(<<"/list/0/a">>, ExampleJSON).
1
2> json:get([list, 0, a], ExampleJSON).
1
3> GetJSONFun = json:get([list, 0, a]).
#Fun<json.0.18938731>
4> GetJSONFun(ExampleJSON).
1
1> json:add([addition], <<"1+1">>, ExampleJSON).
#{<<"addition">> => <<"1+1">>,
<<"awesome">> => true,
<<"library">> => <<"json">>,
<<"list">> => [#{<<"a">> => 1},#{<<"b">> => 2},#{<<"c">> => 3}]}
2> json:add([recursion], ExampleJSON, ExampleJSON).
#{<<"awesome">> => true,
<<"library">> => <<"json">>,
<<"list">> => [#{<<"a">> => 1},#{<<"b">> => 2},#{<<"c">> => 3}],
<<"recursion">> => #{<<"awesome">> => true,
<<"library">> => <<"json">>,
<<"list">> => [#{<<"a">> => 1},#{<<"b">> => 2},#{<<"c">> => 3}]}}
3> json:add([map], #{<<"test2">> => 50}, ExampleJSON).
#{<<"awesome">> => true,
<<"library">> => <<"json">>,
<<"list">> => [#{<<"a">> => 1},#{<<"b">> => 2},#{<<"c">> => 3}],
<<"map">> => #{<<"test2">> => 50}}
4> json:add(<<"/listtest">>, [50, <<"listadd">>, testatom], ExampleJSON).
#{<<"awesome">> => true,
<<"library">> => <<"json">>,
<<"list">> => [#{<<"a">> => 1},#{<<"b">> => 2},#{<<"c">> => 3}],
<<"listtest">> => [50,<<"listadd">>,testatom]}
1> json:remove([list, 2], ExampleJSON).
#{<<"awesome">> => true,
<<"library">> => <<"json">>,
<<"list">> => [#{<<"a">> => 1},#{<<"b">> => 2}]}
1> json:replace([awesome], <<"json in erlang!">> ,ExampleJSON).
#{<<"awesome">> => <<"json in erlang!">>,
<<"library">> => <<"json">>,
<<"list">> => [#{<<"a">> => 1},#{<<"b">> => 2},#{<<"c">> => 3}]}
1> json:copy([list],[copiedlist],ExampleJSON).
#{<<"awesome">> => true,
<<"copiedlist">> => [#{<<"a">> => 1},#{<<"b">> => 2},#{<<"c">> => 3}],
<<"library">> => <<"json">>,
<<"list">> => [#{<<"a">> => 1},#{<<"b">> => 2},#{<<"c">> => 3}]}
1> json:move([library], [newlibrary], ExampleJSON).
#{<<"awesome">> => true,
<<"list">> => [#{<<"a">> => 1},#{<<"b">> => 2},#{<<"c">> => 3}],
<<"newlibrary">> => <<"json">>}
ExampleJSON = json:from_binary(<<"{\"library\": \"json\", \"awesome\": true, \"list\": [{\"a\": 1}, {\"b\": 2}, {\"c\": 3} ]}">>).
#{<<"awesome">> => true,
<<"library">> => <<"json">>,
<<"list">> => [#{<<"a">> => 1},#{<<"b">> => 2},#{<<"c">> => 3}]}
2> PatchEx = #{<<"op">> => <<"add">>, <<"path">> => [patchtest], <<"value">> => true}.
#{<<"op">> => <<"add">>,<<"path">> => [patchtest],<<"value">> => true}
3> json:patch([PatchEx], ExampleJSON).
#{<<"awesome">> => true,
<<"library">> => <<"json">>,
<<"list">> => [#{<<"a">> => 1},#{<<"b">> => 2},#{<<"c">> => 3}],
<<"patchtest">> => true}
1> JSON = #{<<"foo">> => <<"bar">>}.
#{<<"foo">> => <<"bar">>}
2> json:fold([json:test(<<"/foo">>, <<"bar">>),
2> json:copy(<<"/foo">>, <<"/qux">>),
2> json:test(<<"/qux">>, <<"bar">>),
2> json:replace(<<"/qux">>, <<"baz">>),
2> json:remove(<<"/foo">>),
2> json:move(<<"/qux">>, <<"/foo">>),
2> json:test(<<"/foo">>, <<"baz">>)
2> ], JSON).
#{<<"foo">> => <<"baz">>}
2> json:keys([], ExampleJSON).
[<<"awesome">>,<<"library">>,<<"list">>]
a high level library for erlang 17.0+. leverages the new maps to support the json spec
path() :: binary() | [atom() | integer() | binary()]
json() :: #{binary() => json()}
| [json()]
| integer()
| float()
| binary()
| true
| false
| null
from_binary(JSON) -> json()
JSON = binary()
uses jsx's decoder/3
to convert a binary()
to json()
to_binary(JSON) -> binary()
JSON = json()
uses jsx's encode
to convert json()
to binary()
minify(JSON) -> binary()
JSON = json()
removes all non-semantic whitespace from a json binary
prettify(JSON) -> binary()
JSON = json()
formats a json binary for output with spaces and newlines
get(Path, JSON) -> json()
get(Path) -> fun((JSON) -> json())
Path = path()
JSON = json()
get json()
at supplied path in JSON, or return an anonymous function to be applied to a given JSON value
add(Path, Value, JSON) -> json()
add(Path, Value) -> fun((JSON) -> json())
Path = path()
Value = json()
JSON = json()
add Value at supplied Path in JSON, or return an anonymous function to do the same for a supplied JSON value
remove(Path, JSON) -> json()
remove(Path) -> fun((JSON) -> json())
Path = path()
JSON = json()
remove json() at Path in JSON, or return an anonymous function to do the same for a supplied JSON value
replace(Path, Value, JSON) -> json()
replace(Path, Value) -> fun((JSON) -> json())
Path = path()
Value = json()
JSON = json()
replace Path in JSON with Value, or return an anonymous function to do the same for a supplied JSON value
copy(From, To, JSON) -> json()
copy(From, To) -> fun((JSON) -> json())
From = path()
To = path()
JSON = json()
copy a json() term From a path To another path in JSON, or return an anonymous function to do the same for a supplied JSON value
move(From, To, JSON) -> json()
move(From, To) -> fun((JSON) -> json())
From = path()
To = path()
JSON = json()
Move a json() term From a path To another path in JSON, or return an anonymous function to do the same for a supplied JSON.
test(Path, Value, JSON) -> json()
test(Path, Value) -> fun((JSON) -> json())
Path = path()
Value = json()
JSON = json()
test the existence of Value at a given Path in JSON, or return an anonymous function to do the same for a supplied JSON value
apply(Path, Fun, JSON) -> json()
apply(Path, Fun) -> fun((JSON) -> json())
apply function Fun at a given Path in JSON, or return an anonymous function to do the same for a supplied JSON value
patch(Ops, JSON) -> json()
Ops = [#{<<"op">> => Op, <<"path">> => Path, Arg => Value}]
JSON = json()
Op = #{<<"op">> => <<"add">>, <<"path">> => Path, <<"value">> => Value}
| #{<<"op">> => <<"remove">>, <<"path">> => Path}
| #{<<"op">> => <<"replace">>, <<"path">> => Path, <<"value">> => Value}
| #{<<"op">> => <<"copy">>, <<"path">> => Path, <<"from">> => Path}
| #{<<"op">> => <<"move">>, <<"path">> => Path, <<"from">> => Path}
| #{<<"op">> => <<"test">>, <<"path">> => Path, <<"value">> => Value}
Path = path()
Value = json()
patches JSON using the methods supplied by the maps in Ops.
fold(Funs, JSON) -> json().
Funs = [function()]
JSON = json()
fold a list of functions, Funs, over JSON
keys(Path, JSON) -> [binary()]
return a list of binary() keys at Path in JSON
bobthenameless for writing the documentation