{"id":20436142,"url":"https://github.com/potatosalad/erlang-ojson","last_synced_at":"2026-03-08T10:37:35.432Z","repository":{"id":57544433,"uuid":"91620271","full_name":"potatosalad/erlang-ojson","owner":"potatosalad","description":"Ordered JSON (OJSON) - deterministic or stable serialization","archived":false,"fork":false,"pushed_at":"2017-05-17T21:57:28.000Z","size":116,"stargazers_count":10,"open_issues_count":1,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-03-14T18:18:58.801Z","etag":null,"topics":["deterministic","elixir","erlang","hex","json","ordered","rebar3","stable"],"latest_commit_sha":null,"homepage":null,"language":"Erlang","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/potatosalad.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-17T21:08:42.000Z","updated_at":"2024-03-10T10:44:05.000Z","dependencies_parsed_at":"2022-08-27T05:13:14.677Z","dependency_job_id":null,"html_url":"https://github.com/potatosalad/erlang-ojson","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/potatosalad%2Ferlang-ojson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/potatosalad%2Ferlang-ojson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/potatosalad%2Ferlang-ojson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/potatosalad%2Ferlang-ojson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/potatosalad","download_url":"https://codeload.github.com/potatosalad/erlang-ojson/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248637589,"owners_count":21137534,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deterministic","elixir","erlang","hex","json","ordered","rebar3","stable"],"created_at":"2024-11-15T08:39:43.178Z","updated_at":"2026-03-08T10:37:30.410Z","avatar_url":"https://github.com/potatosalad.png","language":"Erlang","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Ordered JSON (OJSON)\n\n[![Build Status](https://travis-ci.org/potatosalad/erlang-ojson.svg?branch=master)](https://travis-ci.org/potatosalad/erlang-ojson) [![Hex.pm](https://img.shields.io/hexpm/v/ojson.svg)](https://hex.pm/packages/ojson)\n\nOrdered JSON (JSON) is a deterministic or stable JSON encoder and decoder for Erlang and Elixir.\n\nOJSON is similar to projects like [fast-stable-stringify](https://github.com/nickyout/fast-stable-stringify) and [json-stable-stringify](https://github.com/substack/json-stable-stringify) which allows for deterministic serialization of object key order.\n\nBefore you say, [\"JSON is unordered!\"](https://tools.ietf.org/html/rfc7159#section-1), the initial use-case for this library is with cryptographic operations, which rely on stable input for authentication and verification in serialized form.  The ordering only applies to the encoding/serialization operations.\n\nInitially based on [devinus/poison](https://github.com/devinus/poison), but rewritten for use as a dependency for Erlang or Elixir projects.\n\n## Installation\n\nIf [using Hex](https://hex.pm/), the package can be installed\nby adding `ojson` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [{:ojson, \"~\u003e 1.0.0\"}]\nend\n```\n\nIf [using rebar3](http://www.rebar3.org/), the package can be installed by adding `ojson` to you list of dependencies in `rebar.config`:\n\n```erlang\n{deps, [\n  {ojson, \".*\", {git, \"git://github.com/potatosalad/erlang-ojson.git\", {branch, \"master\"}}}\n]}.\n```\n\nThe [HexDocs](https://hexdocs.pm) can\nbe found at [https://hexdocs.pm/ojson](https://hexdocs.pm/ojson).\n\n## Usage\n\n##### Basic Usage\n\n_Elixir_\n\n```elixir\niex\u003e OJSON.encode(%{\"z\" =\u003e 1, \"a\" =\u003e 2})\n{:ok, \"{\\\"a\\\":2,\\\"z\\\":1}\"}\n\niex\u003e OJSON.encode!(%{\"z\" =\u003e 1, \"a\" =\u003e 2})\n\"{\\\"a\\\":2,\\\"z\\\":1}\"\n\niex\u003e OJSON.encode_to_iodata(%{\"z\" =\u003e 1, \"a\" =\u003e 2})\n{:ok, [123, [[34, [\"a\"], 34], 58, \"2\"], [[44, [34, [\"z\"], 34], 58, \"1\"]], 125]}\n\niex\u003e OJSON.encode_to_iodata!(%{\"z\" =\u003e 1, \"a\" =\u003e 2})\n[123, [[34, [\"a\"], 34], 58, \"2\"], [[44, [34, [\"z\"], 34], 58, \"1\"]], 125]\n\niex\u003e OJSON.decode(\"{\\\"a\\\":2,\\\"z\\\":1}\")\n{:ok, %{\"a\" =\u003e 2, \"z\" =\u003e 1}}\n\niex\u003e OJSON.decode!(\"{\\\"a\\\":2,\\\"z\\\":1}\")\n%{\"a\" =\u003e 2, \"z\" =\u003e 1}\n```\n\n_Erlang_\n\n```erlang\nojson:encode(#{ \u003c\u003c\"z\"\u003e\u003e =\u003e 1, \u003c\u003c\"a\"\u003e\u003e =\u003e 2 }).\n% {ok, \u003c\u003c\"{\\\"a\\\":2,\\\"z\\\":1}\"\u003e\u003e}\n\nojson:'encode!'(#{ \u003c\u003c\"z\"\u003e\u003e =\u003e 1, \u003c\u003c\"a\"\u003e\u003e =\u003e 2 }).\n% \u003c\u003c\"{\\\"a\\\":2,\\\"z\\\":1}\"\u003e\u003e\n\nojson:encode_to_iodata(#{ \u003c\u003c\"z\"\u003e\u003e =\u003e 1, \u003c\u003c\"a\"\u003e\u003e =\u003e 2 }).\n% {ok, [123,[[34,[\u003c\u003c\"a\"\u003e\u003e],34],58,\u003c\u003c\"2\"\u003e\u003e],[[44,[34,[\u003c\u003c\"z\"\u003e\u003e],34],58,\u003c\u003c\"1\"\u003e\u003e]],125]}\n\nojson:'encode_to_iodata!'(#{ \u003c\u003c\"z\"\u003e\u003e =\u003e 1, \u003c\u003c\"a\"\u003e\u003e =\u003e 2 }).\n% [123,[[34,[\u003c\u003c\"a\"\u003e\u003e],34],58,\u003c\u003c\"2\"\u003e\u003e],[[44,[34,[\u003c\u003c\"z\"\u003e\u003e],34],58,\u003c\u003c\"1\"\u003e\u003e]],125]\n\nojson:decode(\u003c\u003c\"{\\\"a\\\":2,\\\"z\\\":1}\"\u003e\u003e).\n% {ok, #{\u003c\u003c\"a\"\u003e\u003e =\u003e 2, \u003c\u003c\"z\"\u003e\u003e =\u003e 1}}\n\nojson:'decode!'(\u003c\u003c\"{\\\"a\\\":2,\\\"z\\\":1}\"\u003e\u003e).\n% #{\u003c\u003c\"a\"\u003e\u003e =\u003e 2, \u003c\u003c\"z\"\u003e\u003e =\u003e 1}\n```\n\n##### Deterministic Example\n\n_Elixir_\n\n```elixir\n# Let's first generate a map with 32 key/value pairs:\niex\u003e map = for key \u003c- :lists.seq(0, 32), into: %{}, do: {String.pad_leading(:erlang.integer_to_binary(key, 16), 2, \"0\"), 0}\n%{\"10\" =\u003e 0, \"0E\" =\u003e 0, \"14\" =\u003e 0, \"12\" =\u003e 0, \"16\" =\u003e 0, \"07\" =\u003e 0, \"1C\" =\u003e 0,\n  \"06\" =\u003e 0, \"19\" =\u003e 0, \"13\" =\u003e 0, \"04\" =\u003e 0, \"1E\" =\u003e 0, \"1F\" =\u003e 0, \"11\" =\u003e 0,\n  \"15\" =\u003e 0, \"17\" =\u003e 0, \"1A\" =\u003e 0, \"0A\" =\u003e 0, \"18\" =\u003e 0, \"0C\" =\u003e 0, \"00\" =\u003e 0,\n  \"0B\" =\u003e 0, \"09\" =\u003e 0, \"02\" =\u003e 0, \"0D\" =\u003e 0, \"1D\" =\u003e 0, \"20\" =\u003e 0, \"03\" =\u003e 0,\n  \"0F\" =\u003e 0, \"1B\" =\u003e 0, \"01\" =\u003e 0, \"05\" =\u003e 0, \"08\" =\u003e 0}\n\n# Notice that the key/value pairs are unordered.\n# Now let's encode our unordered map into an ordered JSON string:\niex\u003e OJSON.encode!(map)\n\"{\\\"00\\\":0,\\\"01\\\":0,\\\"02\\\":0,\\\"03\\\":0,\\\"04\\\":0,\\\"05\\\":0,\\\"06\\\":0,\\\"07\\\":0,\\\"08\\\":0,\\\"09\\\":0,\\\"0A\\\":0,\\\"0B\\\":0,\\\"0C\\\":0,\\\"0D\\\":0,\\\"0E\\\":0,\\\"0F\\\":0,\\\"10\\\":0,\\\"11\\\":0,\\\"12\\\":0,\\\"13\\\":0,\\\"14\\\":0,\\\"15\\\":0,\\\"16\\\":0,\\\"17\\\":0,\\\"18\\\":0,\\\"19\\\":0,\\\"1A\\\":0,\\\"1B\\\":0,\\\"1C\\\":0,\\\"1D\\\":0,\\\"1E\\\":0,\\\"1F\\\":0,\\\"20\\\":0}\"\n```\n\n_Erlang_\n\n```erlang\n%% Let's first generate a map with 32 key/value pairs:\nMap = maps:from_list([{case erlang:integer_to_binary(I, 16) of \u003c\u003cB\u003e\u003e -\u003e \u003c\u003c$0,B\u003e\u003e; B -\u003e B end, 0} || I \u003c- lists:seq(0, 32)]).\n% #{\u003c\u003c\"10\"\u003e\u003e =\u003e 0,\u003c\u003c\"0E\"\u003e\u003e =\u003e 0,\u003c\u003c\"14\"\u003e\u003e =\u003e 0,\u003c\u003c\"12\"\u003e\u003e =\u003e 0,\n%   \u003c\u003c\"16\"\u003e\u003e =\u003e 0,\u003c\u003c\"07\"\u003e\u003e =\u003e 0,\u003c\u003c\"1C\"\u003e\u003e =\u003e 0,\u003c\u003c\"06\"\u003e\u003e =\u003e 0,\n%   \u003c\u003c\"19\"\u003e\u003e =\u003e 0,\u003c\u003c\"13\"\u003e\u003e =\u003e 0,\u003c\u003c\"04\"\u003e\u003e =\u003e 0,\u003c\u003c\"1E\"\u003e\u003e =\u003e 0,\n%   \u003c\u003c\"1F\"\u003e\u003e =\u003e 0,\u003c\u003c\"11\"\u003e\u003e =\u003e 0,\u003c\u003c\"15\"\u003e\u003e =\u003e 0,\u003c\u003c\"17\"\u003e\u003e =\u003e 0,\n%   \u003c\u003c\"1A\"\u003e\u003e =\u003e 0,\u003c\u003c\"0A\"\u003e\u003e =\u003e 0,\u003c\u003c\"18\"\u003e\u003e =\u003e 0,\u003c\u003c\"0C\"\u003e\u003e =\u003e 0,\n%   \u003c\u003c\"00\"\u003e\u003e =\u003e 0,\u003c\u003c\"0B\"\u003e\u003e =\u003e 0,\u003c\u003c\"09\"\u003e\u003e =\u003e 0,\u003c\u003c\"02\"\u003e\u003e =\u003e 0,\n%   \u003c\u003c\"0D\"\u003e\u003e =\u003e 0,\u003c\u003c\"1D\"\u003e\u003e =\u003e 0,\u003c\u003c\"20\"\u003e\u003e =\u003e 0,\u003c\u003c\"03\"\u003e\u003e =\u003e 0,\n%   \u003c\u003c\"0F\"\u003e\u003e =\u003e 0,\u003c\u003c\"1B\"\u003e\u003e =\u003e 0,\u003c\u003c\"01\"\u003e\u003e =\u003e 0,\u003c\u003c\"05\"\u003e\u003e =\u003e 0,\n%   \u003c\u003c\"08\"\u003e\u003e =\u003e 0}\n\n%% Notice that the key/value pairs are unordered.\n%% Now let's encode our unordered map into an ordered JSON string:\nojson:'encode!'(Map).\n% \u003c\u003c\"{\\\"00\\\":0,\\\"01\\\":0,\\\"02\\\":0,\\\"03\\\":0,\\\"04\\\":0,\\\"05\\\":0,\\\"06\\\":0,\\\"07\\\":0,\\\"08\\\":0,\\\"09\\\":0,\\\"0A\\\":0,\\\"0B\\\":0,\\\"0C\\\":0,\\\"0D\\\":0,\\\"0E\\\":0,\\\"0F\\\":0,\\\"10\\\":0,\\\"11\\\":0,\\\"12\\\":0,\\\"13\\\":0,\\\"14\\\":0,\\\"15\\\":0,\\\"16\\\":0,\\\"17\\\":0,\\\"18\\\":0,\\\"19\\\":0,\\\"1A\\\":0,\\\"1B\\\":0,\\\"1C\\\":0,\\\"1D\\\":0,\\\"1E\\\":0,\\\"1F\\\":0,\\\"20\\\":0}\"\u003e\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpotatosalad%2Ferlang-ojson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpotatosalad%2Ferlang-ojson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpotatosalad%2Ferlang-ojson/lists"}