Skip to content

Instantly share code, notes, and snippets.

@cmiles74
Created November 9, 2008 15:48
Show Gist options
  • Save cmiles74/23299 to your computer and use it in GitHub Desktop.
Save cmiles74/23299 to your computer and use it in GitHub Desktop.
Clojure code for transforming sequences into JSON data
;
; Provides functions for transforming data into JSON objects.
;
; This is Clojure code.
;
(defn data-to-json [list-this]
"Transforms a list into a string of JSON data"
(cond
; handle sequences and vectors
(or (seq? list-this) (vector? list-this))
(apply str
(concat "["
(interpose ", "
(map data-to-json list-this)) "]"))
; handle maps
(map? list-this)
(apply str
(concat "{"
(interpose ", "
(map #(str "\"" (name (first %)) "\": "
(data-to-json (second %))) list-this)) "}"))
; handle atoms
:else (str "\"" list-this "\"")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment