Created
February 17, 2016 18:17
-
-
Save algal/f7863da2a5425cfe1600 to your computer and use it in GitHub Desktop.
Quick JSON to CSV converter in Clojure, for use with boot-clj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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