JSON and term formats

Contents
Terms everywhere
JSON libraries
Base64 and friends
Which format

Terms everywhere

Any Erlang term can turn into a binary and back. term_to_binary and binary_to_term are the pair every Erlang developer knows.

The format is stable across OTP releases within reason, and it round-trips every value exactly.

1> term_to_binary({ok, [1, 2]}).

<<131,104,2,100,0,2,111,107,108,0,0,0,2,1,2>>

2> binary_to_term(term_to_binary({ok, [1, 2]})).

{ok,[1,2]}

JSON libraries

Erlang ships no JSON in the core, so projects add jiffy or jsx through rebar3. Both encode maps and lists and decode JSON into maps.

The decode answer is a map, so the maps tools of lesson 14 take over from there.

1> jiffy:encode(#{name => "Ada"}).

<<"{\"name\":\"Ada\"}">>

2> jiffy:decode("{\"name\":\"Ada\"}", [return_maps]).

#{name => "Ada"}

Base64 and friends

The base64 module encodes binaries to ASCII text and back. It is the standard way to carry binary payloads over text-only channels.

The erlang module also packs and unpacks smaller bases when the payload is tiny.

1> base64:encode("hello").

<<"aGVsbG8=">>

2> base64:decode("aGVsbG8=").

<<"hello">>

Which format

Use term_to_binary inside your own cluster, JSON at the edges with other languages, and Base64 when text channels force it.

The rule of thumb: match the format to the reader, never to the writer.

% inside the cluster: term_to_binary

% across languages: jiffy:encode

% text-only wire: base64:encode

Next: Processes and message passing

Article author: Arthur Isaev

Related articles
Erlang
Erlang course: zero to hero in 50 lessons
What is Erlang and where it runs
Install Erlang and check the version
erl: the interactive shell
Your first Erlang script
Variables and single assignment
Numbers
Atoms
Strings and binaries
Truthiness: only false and nil
Conditions: case and if
Recursion replaces loops
Lists
Tuples
Maps
Functions and clauses
Pattern matching
Guards
Records
Exceptions: try and catch
Modules, exports and imports
Funs: anonymous functions
Higher-order functions
Keyword lists
Erlang idioms and clean code
Rebar3 builds
Testing with EUnit
Debugging
Encodings and Unicode
Time and date
JSON and term formats
Processes and message passing
Spawn and links
Monitors and exits
GenServer
Supervisors
OTP applications
Files and IO
Sockets
HTTP clients
io:format and output
Let it crash: error handling
ETS tables
Timers and timeouts
Code loading
Distributed Erlang
Capstone: todo CLI
Capstone: TCP server
Performance
Modern Erlang tour
Hero roadmap: the whole course on one page
Install Erlang on Windows 11
Install Erlang on Ubuntu
Install Erlang on Rocky Linux
Install Erlang on macOS
Install Erlang on FreeBSD
Erlang in Docker
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: