/jet

Primary LanguageErlang

Json Erlang Tools

Json Pointers

Get

Json = jet:decode(binary_to_list('{"user":{"profiles":[{"name":"anthony"}]}}')),
<<"anthony">> = jet_pointer:get("/user/profiles/0/name", Json),
#{ <<"name">> := <<"anthony">> }  = jet_pointer:get("/user/profiles/0", Data).

Put

Json = jet:decode(binary_to_list('{"user":{"profiles":[{"name":"anthony"}]}}')),
ok = jet_pointer:put("/user/profiles/0/name", <<"andrew">>, Json),
<<"andrew">> = jet_pointer:get("/user/profiles/0/name", Json).

Json Schema

Validate

GoodJson = jet:decode(binary_to_list('{"name":"anthony"}')),
BadJson = jet:decode(binary_to_list('{"name":10}')),

Schema = jet:decode(binary_to_list('{"type":"object","properties":{"name":{"type":"string"}}')),
true = jet_schema:validate(Schema, GoodJson),
{false, _Errors} = jet_schema:validate(Schema, BadJson),

SchemaFilename = "./priv/schema.v1.person.json",
true = jet_schema:validate(SchemaFilename, GoodJson),
{false, _Errors} = jet_schema:validate(SchemaFilename, BadJson),

SchemaUrl = "http://domain.com/schemas/v1.person.json",
true = jet_schema:validate(SchemaUrl, GoodJson),
{false, _Errors} = jet_schema:validate(SchemaUrl, BadJson).

Transform according to Json Schema

Json = jet:decode(binary_to_list('{"name":"anthony"}')),
Schema = jet:decode(binary_to_list('{"type":"object","properties":{"name":{"type":"string"}}')),
{ok, #{ <<"name">> => <<"anthony">> }} = jet_schema:transform(Schema, Json),
Opts = #{ key_type => atom, string_type => list },
{ok, #{ name => "anthony" }} = jet_schema:transform(Schema, Json, Opts).
Error Format

Will follow the specification to be determined as part of JSON Schema standard.

#{ keyword := "oneOf",
   schema_path := "#/oneOf",
   message := "the instance did not match any of the subschemas",
   errors := [ #{ keyword := "additionalProperties",
                  schema_path := "#/oneOf/0/additionalProperties",
                  data_path := "/age",
                  message := "additional properties are not allowed" },
               #{ keyword := "enum",
                  schema_path := "#/oneOf/1/properties/membershipType/enum",
                  data_path := "/membershipType",
                  message := "value does not match any of the allowed values" } ] }.

Pattern Matching

Json = jet:decode(binary_to_list('{"name":"anthony"}')),
MatchList = jet:decode(binary_to_list('[{"case":"has_name", "type":"object","properties":{"name":{"type":"string"}}]')),
{ok, <<"has_name">>} = jet:match(MatchList, Json).
Match Rules
Json = jet:decode(binary_to_list('{"name":"anthony"}')),
MatchList = jet:decode(binary_to_list('[{"case":"has_name", "type":"object","properties":{"name":{"type":"string"}}]')),
{ok, <<"has_name">>} = jet:match(MatchList, Json).