Skip to content

clojure.walk/stringify-keys

View this page on ClojureDocs

Type: function Added: Clojure 1.1 Examples: 3 Runnable: 3
([m])

Recursively transforms all map keys from keywords to strings.

Examples

by philos99 on
by eunmin on
by kevo1ution on

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.