# Buult-in JSON decoding keys as existing atoms

**URL:** <https://erlangforums.com/t/buult-in-json-decoding-keys-as-existing-atoms/3863>\
**Category:** Proposals: Ideas\
**Tags:** erlang-otp, json\
**Created:** [July 26, 2024, 9:08am UTC](https://erlangforums.com/t/buult-in-json-decoding-keys-as-existing-atoms/3863 "2024-07-26T09:08:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![eproxus](https://erlangforums.com/user_avatar/erlangforums.com/eproxus/32/452_2.png) [@eproxus](https://erlangforums.com/u/eproxus)\
**Post date:** [July 26, 2024, 9:08am UTC](https://erlangforums.com/t/buult-in-json-decoding-keys-as-existing-atoms/3863/1 "2024-07-26T09:08:23Z")

</div>

A common feature that almost all existing JSON libraries support is converting the resulting keys to atoms. While the new `json` module technically supports this, the interface is quite cumbersome:

```erlang
Push = fun(Key, Value, Acc) -> [{binary_to_existing_atom(Key), Value} | Acc] end,
json:decode(<<"{\"foo\": 1}">>, ok, #{object_push => Push}).

```

It would be nice with a simpler API for the most common use cases, such as:

```erlang
json:decode(<<"{\"foo\": 1}">>, #{keys => existing_atom}).

```

Perhaps this has already been thought of. Are there any existing plans to add such an interface?

---

<div class="post-metadata">

**Author:** ![starbelly](https://erlangforums.com/user_avatar/erlangforums.com/starbelly/32/209_2.png) [@starbelly](https://erlangforums.com/u/starbelly)\
**Post date:** [July 27, 2024, 7:18pm UTC](https://erlangforums.com/t/buult-in-json-decoding-keys-as-existing-atoms/3863/2 "2024-07-27T19:18:52Z")

</div>

I’ve been busy 😄 I didn’t get a chance to look at the api, I’m surprised what you’re suggesting wasn’t done already and that the api of `Jason` or other libraries wasn’t essentially mirrored considering it’s success.

Sounds like an easy win to me.

---

<div class="post-metadata">

**Author:** ![michalmuskala](https://erlangforums.com/user_avatar/erlangforums.com/michalmuskala/32/247_2.png) [@michalmuskala](https://erlangforums.com/u/michalmuskala)\
**Post date:** [August 27, 2024, 4:21pm UTC](https://erlangforums.com/t/buult-in-json-decoding-keys-as-existing-atoms/3863/3 "2024-08-27T16:21:14Z")

</div>

This was a sort-of intentional omission. The new API offered by the `json` module is strictly more powerful, even if more verbose.  
The complexity of supporting both APIs come when you pass some option from one and some options from the other. For example, it’s not clear what a call like this would mean:

```erlang
json:decode(<<"{\"foo\": 1}">>, #{keys => existing_atom, object_push => ...}).

```

I would see two possible, and in my mind undesirable, outcomes:

1. Combining options from the two APIs is not supported - this means that as soon as you needed to use the “advanced” API, you’d need large code changes
2. We define some rules for how the options are combined - this means that the result could be not intuitive

For this reason, I decided to conservatively err on the side of smaller, simpler API, even if there’s some extra verbosity.

---

<div class="post-metadata">

**Author:** ![nzok](https://erlangforums.com/letter_avatar_proxy/v4/letter/n/4af34b/32.png) [@nzok](https://erlangforums.com/u/nzok)\
**Post date:** [August 28, 2024, 6:18am UTC](https://erlangforums.com/t/buult-in-json-decoding-keys-as-existing-atoms/3863/4 "2024-08-28T06:18:48Z")

</div>

keys =\> existing\_atom is already the wrong approach, by the way.  
Why do I say that?  
Why do we want a JSON object to arrive with atoms as keys?  
Because we are expecting to MATCH those atoms.  
There is a protocol, an agreement between sender and receiver,  
according to which certain keys are expected to be present,  
and atoms are being used in their Erlang-traditional role of  
representing protocol tokens.  
What we want then is something like  
expected\_keys =\> [plangent,ochre,triskelion,…],  
other\_keys =\> binary  
where the JSON keys to be converted to atoms are NOT any old JSON  
strings that _happen_ to have a corresponding atom in Erlang, but  
the keys we _expect_ as part of the protocol.

This also addresses another issue. Consider four policies:  
(A) Always convert JSON keys to atoms. This would be a good policy if Erlang either garbage collected the atom table or split the atoms as recommended by EEP 20.  
(H) Convert JSON keys to atoms HAPHAZARDLY, in a way that depends on the History of the current node.  
(P) Convert JSON keys that are part of the expected PROTOCOL to atoms  
(N) Never convert JSON keys to atoms.  
Out of these four policies, three of them guarantee that the same JSON object decoded by processes in different nodes will decode as the SAME Erlang term, and the fourth does not can cannot make any such guarantee. Three of those four policies guarantee that if you decode a JSON object and stuff it in ETS or DETS or anything that preserves Erlang structure, and at a later time convert the same JSON object with the same options and go looking for the result in ETS or DETS or whatever, you will find it. The fourth does not and cannot make any such guarantee.

Of course it’s existing\_atom that’s the dangerous one.

With the advent of maps, option (P) became practical. It’s arguably better from a software engineering point of view.

This is true of list\_to\_existing\_atom/1 generally. It’s a sticky bandage (to avoid trademarks). A stopgap. Is there _any_ use of list\_to\_existing\_atom/1 that would not be better expressed as list\_to\_expected\_atom(List, Atoms), (Or for efficiency via a two-step process, first converting a list of atoms to an unspecified data structure – actually a map – and then looking a list of characters up in that unspecified data structure.) Perhaps this deserves an EEP. I do know that when list\_to\_existing\_atom/1 was introduced I saw it as a temporary expedient that would go away soon. It’s _not_ a good thing to build on.

---

<div class="post-metadata">

**Author:** ![jhogberg](https://erlangforums.com/user_avatar/erlangforums.com/jhogberg/32/179_2.png) [@jhogberg](https://erlangforums.com/u/jhogberg)\
**Post date:** [August 28, 2024, 8:40am UTC](https://erlangforums.com/t/buult-in-json-decoding-keys-as-existing-atoms/3863/5 "2024-08-28T08:40:29Z")

</div>

> [@nzok](#):
>
> Perhaps this deserves an EEP.

Yes, I very much like the idea of `list_to_expected_atom(List, Atoms)` and its binary counterpart. In addition to the reasons you’ve listed, it also avoids a really silly corner case where one or more of the desired atoms have been _optimized out_, resulting in a surprising crash that is not obvious how to debug – the atom is there in the source code!

---

<div class="post-metadata">

**Author:** ![dch](https://erlangforums.com/user_avatar/erlangforums.com/dch/32/962_2.png) [@dch](https://erlangforums.com/u/dch)\
**Post date:** [August 28, 2024, 9:21am UTC](https://erlangforums.com/t/buult-in-json-decoding-keys-as-existing-atoms/3863/6 "2024-08-28T09:21:43Z")

</div>

Yea this sounds pretty good, where the exact schema is already known at compile time.

Generally this seems like the first step towards JSON schema validation ala [GitHub - vstakhov/libucl: Universal configuration library parser](https://github.com/vstakhov/libucl?tab=readme-ov-file#validation) & similar libraries using JSON schema. [https://json-schema.org/](https://json-schema.org/) where the shape and structure, possibly nested, needs to be validated too.

I have written a NIF for libUCL parsing already, and it would be easy to extend this to validate against a provided JSON schema.

Dave

---

<div class="post-metadata">

**Author:** ![michalmuskala](https://erlangforums.com/user_avatar/erlangforums.com/michalmuskala/32/247_2.png) [@michalmuskala](https://erlangforums.com/u/michalmuskala)\
**Post date:** [August 28, 2024, 10:13am UTC](https://erlangforums.com/t/buult-in-json-decoding-keys-as-existing-atoms/3863/7 "2024-08-28T10:13:03Z")

</div>

The good thing about the API in the `json` module is that it allows building any of those options on top of it

---

<div class="post-metadata">

**Author:** ![LostKobrakai](https://erlangforums.com/user_avatar/erlangforums.com/lostkobrakai/32/453_2.png) [@LostKobrakai](https://erlangforums.com/u/LostKobrakai)\
**Post date:** [August 28, 2024, 10:17am UTC](https://erlangforums.com/t/buult-in-json-decoding-keys-as-existing-atoms/3863/8 "2024-08-28T10:17:15Z")

</div>

This reminds me very much of:

> **[On the complexity of JSON serialization](https://einarwh.wordpress.com/2020/05/08/on-the-complexity-of-json-serialization/)**
>
> I vented a bit on Twitter the other day about my frustrations with JSON serialization in software development. I thought I’d try to write it out in a bit more detail. I’m going to embra…

To me wanting to get atoms back is the first tinies step in moving from  
json text ↔ json object model towards json text ↔ your data model. I can certainly see the appeal, but keeping the step separate makes things more maintainable.

> [@michalmuskala](#):
>
> The good thing about the API in the `json` module is that it allows building any of those options on top of it

I was just about to suggest that. There could be APIs, where e.g. given a list of expected atoms it would return the necessary set of options for the `json:decode` call.
