Search code examples
emacselisp

Emacs - represent JSON-like structures


What would be the canonical way for emacs to represent JSON-like structures, or nested hashmaps ?

I have a structure with approximately 25 top-level keys. Each key has no more than a sub-key (ie. the value is another key/value element). Some of the final values are FIFO arrays.

I stated to model this using hash-map, but it feels cumbersome. Now I just stumbled upon assoc-lists, what would be the most appropriate in my case ?

Note : I intend to replicate parinfer in elisp, this part for now, and learn elisp at the same time.


Solution

  • You should use assoc-lists, which are the Emacs standard way of representing a map/dictionary/table. You see them in a lot of places: auto-mode-alist, minor-mode-alist, interpreter-mode-alist, etc. hash-map is only meant for speed, when you have 1000+ entries.

    There's even an official way to convert JSON to an assoc-list:

    (json-read-from-string "{\"foo\": {\"bar\": 5}}")
    => ((foo (bar . 5)))