clojure.walk/stringify-keys¶
Recursively transforms all map keys from keywords to strings.
Examples¶
Note by gongpengjun¶
Recursively transforms all map keys and values from strings to keywords.
(use 'clojure.walk)
(defn stringify-keys-and-vals
"Recursively transforms all map keys and values from keywords to strings."
[m]
(let [kf (fn [[k v]] (if (keyword? k) [(name k) v] [k v]))
vf (fn [[k v]] (if (keyword? v) [k (name v)] [k v]))]
;; only apply to maps
(postwalk (fn [x] (if (map? x) (into {} (map #(-> % kf vf) x)) x)) m)))
Example:
(stringify-keys-and-vals {:a :hello :b :world})
;=> {"a" "hello", "b" "world"}
(stringify-keys-and-vals {:a {:hello :world} :b [{:love :clojure} {:hate :other}]})
;=> {"a" {"hello" "world"}, "b" [{"love" "clojure"} {"hate" "other"}]}
See also¶
Content from the matching ClojureDocs page, with authors credited on each contribution.