Skip to content

Instantly share code, notes, and snippets.

@algal
Created February 17, 2016 18:17
Show Gist options
  • Save algal/f7863da2a5425cfe1600 to your computer and use it in GitHub Desktop.
Save algal/f7863da2a5425cfe1600 to your computer and use it in GitHub Desktop.
Quick JSON to CSV converter in Clojure, for use with boot-clj
#!/usr/bin/env boot
;; needs boot-clj
(set-env! :dependencies '[[org.clojure/data.csv "0.1.3"]
[org.clojure/data.json "0.2.6"]])
(require '[clojure.data.csv :as csv])
(require '[clojure.data.json :as json])
(defn run [in out]
(let [data (json/read in)
all-keys (apply clojure.set/union (map (comp set keys) data))
sorted-keys (sort (vec all-keys))
values-fn (fn [m] (mapv m sorted-keys))
value-rows (map values-fn data)
header-and-rows (vec (cons sorted-keys value-rows))]
(csv/write-csv out header-and-rows)))
(defn -main [& args]
(run *in* *out*))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment