(keyword "abcdef") => :abcdef (defn metric-diff [metric & json-objs] (apply - (map (comp :value (keyword metric)) json-objs)))
Probably does what you want. On Mar 30, 12:04 pm, Avram <aav...@me.com> wrote: > hi, > > I have 2 report files in JSON format like below: > > # file 1: a.json > { "abcdef" : { "value" : "10" } > > # file 2: b.json > { "abcdef" : { "value" : "20" } > > I am using clojure.contrib.json to read each file, and I want a > function that takes in the 2 structures and a metric name (e.g. > abcdef). The function will then look up abcdef in each structure, > find the value of each, and compute the difference. I am running into > an issue, however, that the function needs to take in a string (e.g. > "abcdef") but convert this to a keyword (e.g. :abcdef ). > > Thus I am looking for the opposite of as-str in > clojure.contrib.string, i.e. something that takes a string and creates > a keyword. > Or perhaps there is a better way? > > This is what I have so far: > > (use 'clojure.contrib.json) > (import 'java.io.FileReader) > (def report1 (clojure.contrib.json/read-json (FileReader. > "a.json" )) ) > (def report2 (clojure.contrib.json/read-json (FileReader. > "b.json" )) ) > > ;; Seek out the values from report1 and report2 for the metric desired > ( e.g. :abcdef), and > ;; compute the difference > ;;------------------------------------------------------------------------- > ---- > (defn compute-metric-diff [ json1 json2 :metric ] > (- (:value (json1 :metric )) (:value (json2 :metric)))) > > user=> (defn compute-metric-diff [ json1 json2 :metric ] > (- (:value (json1 :metric )) (:value (json2 :metric)))) > java.lang.Exception: Unsupported binding form: :metric (NO_SOURCE_FILE: > 58) > > ;; I want to be able to call it like so > (compute-metric-diff report1 report2 "abcdef" ) > > Any help appreciated. > > Many thanks, > Avram -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en