Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Helper module for working with Erlang terms representing JSON
branch: master
Failed to load latest commit information.
doc concrete-ize ej
include Added top level empty list and array
src allow fun_match to take any_type
test allow fun_match to take any_type
.gitignore concrete-ize ej
Makefile concrete-ize ej Add small example for complex get to README concrete-ize ej
rebar.config Add cover_enabled true to rebar.config
rebar.config.script concrete-ize ej

ej helps you work with Erlang terms representing JSON

The ej module makes it easier to work with Erlang terms representing JSON in the format returned by jiffy, mochijson2, or ejson. You can use ej:get/2 to walk an object and return a particular value, ej:set/3 to update a value within an object, or ej:delete/2 to remove a value from an object.

In ej, paths into JSON objects are expressed using a tuple of keys like so:

Javascript ej ej:get({"author", "name", "first"}, Obj)

To get started using ej, see ej by example below.

ej also provides a means of validating a JSON object according to a specification you provide. This feature is useful if you need to process JSON request bodies. As a brief example, here’s a specification for a JSON object describing a person and their favorite foods:

  {<<"name">>, {string_match, regex_for(name)}},
  {{opt, <<"nick_name">>}, {string_match, regex_for(name)}},
  {<<"foods">>, {array_map, string}}

ej is independent of the library used for JSON serialization and has no dependencies.

ej by example

ej is best explained by example. Consider the following JSON data (borrowed from

{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}

mochijson2:decode/1 translates the JSON into Erlang terms like this:


And here’s ej in action:

% specify the path you want to access as a tuple of keys (you can use
% strings or binaries)
4> ej:get({"menu", "value"}, Obj).

% you can access list elements by index
> ej:get({"menu", "popup", "menuitem", 2, "onclick"}, Obj).

% The atoms 'first' and 'last' can be used for lists as well
> ej:get({"menu", "popup", "menuitem", first, "value"}, Obj).  

% you can filter a list of objects by specifying a property (key/value
% pair) to match on:
ej:get({"menu", "popup", "menuitem", {select, {"value", "New"}}}, Obj).

% set a value
Obj2 = ej:set({"menu", "id"}, Obj, <<"abc123">>).

% add a value
Obj3 = ej:set({"menu", "new_key"}, Obj, <<"something">>).

% add a value to a list
NewItem = {struct,[{<<"value">>,<<"Save">>}, {<<"onclick">>,<<"SaveDoc()">>}]}.
Obj4 = ej:set({"menu", "popup", "menuitem", new}, Obj, NewItem).

The idea for this helper module was inspired by this thread on the Erlang Questions mailing list and, in particular, by the reply from Richard O’Keefe. Additional motivation from the very similar helper module struct included in the sticky notes example application from the folks at BeeBole.


  • Christopher Brown
  • Christopher Maier
  • John Keiser
  • Sebastian Probst Eide

Build status

Something went wrong with that request. Please try again.