Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure: convert a string to map

Do you know a function that takes a Clojure string and converts it to a map. For example, if the function gets the string

:first "John" :last "Lukas" :city "London"

it returns a map with the previous key-value relations.

like image 282
CrazySynthax Avatar asked Oct 31 '25 10:10

CrazySynthax


1 Answers

You can use the clojure.end/read-string function for this. It makes sure that nobody injects code to your system, but otherwise will parse clojure data structures. Prepend "{" and append "}" to make it a map, that can be parsed that way:

(def stringtoparse ":first \"John\" :last \"Lukas\" :city \"London\"") 

(clojure.edn/read-string (str "{" stringtoparse "}"))
like image 191
Matthias Wimmer Avatar answered Nov 03 '25 00:11

Matthias Wimmer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!