DEV Community

Alexander Oloo
Alexander Oloo

Posted on • Originally published at alexanderoloo.com on

2 1

Fun with Clojure maps

There was a time where I was working exclusively in clojure and using SSR for the frontend. It was a peaceful time. But alas peace doesn’t last for ever. And then I picked up a new full stack project. A project building a SPA.

And JSON said to me: “Peace has cost you your strength. Victory has defeated you”.

The hastle of parsing json Node was all too fresh in my memory. Would Clojure be different?

(def json { :action "link-profiles", 
            :data { :user "alekcz", 
                    :social { :github "https://github.com/alekcz" 
                              :twitter "https://twitter.com/alekcz"}}})

The first time I had to parse a json request I hit a quick google. I was still quite new to Clojure so most problems involved a quick google. And voilà. Getting data from within a map"

So I tried get. And it was ridiculous

(get (get (get json :data) :social) :twitter)

And when something is unwieldy in Clojure there’s probably a more elegant way of doing it. You just need to be willing to look beyond the first google result.

(get-in json [:data :social :twitter])

Oh isn’t that nice. You gotta love the elegance. To be honest I thought that was as good as it got, until I stumbled across this gem. Turns out you can access data in a map using key like so:

(:data json)
; => {:user "alekcz", :social {:github "alekcz", :twitter "alekcz"}}

(:data nil) ; and doing this didn't crash anything.
; nil

It was like magic. Immediately it made me think of the thread-first macro. Maybe? Just maybe we could? Was it too good to be true? Surely not?

(-> json :data :social :twitter)
; => "https://twitter.com/alekcz"

Sublime.

May your build always pass.

Alex

This post is part of the “Advent of Parens”.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more