This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-05-28
Channels
- # admin-announcements (1)
- # beginners (33)
- # boot (35)
- # braveandtrue (1)
- # cider (6)
- # cljs-dev (59)
- # cljsrn (24)
- # clojure (44)
- # clojure-austin (3)
- # clojure-china (1)
- # clojure-russia (13)
- # clojure-spec (63)
- # clojurescript (71)
- # community-development (1)
- # core-async (2)
- # cursive (6)
- # datomic (1)
- # editors-rus (2)
- # emacs (3)
- # hoplon (47)
- # jobs-discuss (3)
- # keechma (1)
- # lein-figwheel (2)
- # om (38)
- # om-next (4)
- # onyx (2)
- # other-languages (63)
- # parinfer (3)
- # planck (4)
- # re-frame (3)
- # reagent (2)
- # slack-help (4)
- # specter (26)
- # tmp-json-parsing (7)
- # uncomplicate (66)
- # yada (7)
Hey folks, is there a good resources for how to grab specific data from complex json structures that contains a vector of hashes?
I’ve been trying to extract data from a rest response for a while and have hit a wall when it comes to filtering out elements of a vector that contain a hash that don’t contain what I want
@dmbennett it's hard to tell where you're stuck better give us an example
@dmbennett: if I'm reading you right your rest response looks something like
(def response {:response
{:foos [{:id 1 :fooname "Bar" :fooval "foo values"}
{:id 2 :fooname "Baz" :fooval "Baz values"}
......... more records]}})
If that's the case then you probably want to map over list of hashes to start. something like
(def my-foos (get-in response [:response :foos])) ;; this gets you just your map of hashes
(def foo-vals (map :fooval my-foos)) ;; from that it's easy to map over your list of maps to extract values
(def filtered-foos (filter (fn [x] (even? (:id x))) my-foos)) ;; use filter with a predicate to limit the records returned
I know for extremely large chunks of data there may be better ways, but for the size of json you typically run into life gets a lot easier to just def (or using let bindings of course if you're doing this in a function) new views into the larger data structure. If you look at the contrived example above to get all the foo-vals
you can see that my-foos
handles the navigation to the data you want, and then the map function extracts the relevant data.@bwstearns: thanks for the response.
example
user=> (map #(* 2 %) (range 1 10))
(2 4 6 8 10 12 14 16 18)
user=> (mapv #(* 2 %) (range 1 10))
[2 4 6 8 10 12 14 16 18]
and also I’m having a hard time understanding the function that I want to call on every vector element
So I'm not sure what workflows you're used to, but for data munging I find it really helpful to just spin up a repl, slurp
in the data and build functions in the repl iteratively and only bother actually saving the ones that work the way I want
Of the histories, I need to check each item for the values :field “status” and return the entire item
@dmbennett: That means that you tried to access an object that is null. Something like:
Car c = null;
c.isStarted(); ->NPE
Hi, those this is indirectly about Closure but I’m experimenting with state and mutation in other languages to get a feel for the problems that Clojure solves and why its solutions are the way that they are. I know that among the goals of referential transparency is to have something like AssertEqual(f(x) : f(x)) always pass. If it doesn’t there is some evil side-effect or mutation lurking in the background. So I wrote some Ruby to cause this problem.
#push returns the state of the array after the push so the left side should come out [1] and the right side to [1, 1].