This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-12-03
Channels
- # adventofcode (23)
- # announcements (2)
- # babashka (54)
- # babashka-sci-dev (2)
- # beginners (14)
- # biff (5)
- # calva (25)
- # cherry (9)
- # clj-kondo (4)
- # clojure-europe (2)
- # clojure-japan (2)
- # clojurescript (1)
- # data-science (6)
- # datascript (3)
- # datomic (1)
- # dev-tooling (25)
- # etaoin (5)
- # events (5)
- # hyperfiddle (1)
- # nbb (2)
- # off-topic (27)
- # shadow-cljs (1)
- # xtdb (7)
Hi Clojurians, I have below response coming back from a Postgres DB function ({:result { "sectionTitle" : "A", "dataPresent":"1" , "asOfDate": "05/17/2021" , "actualDate": "04/14/2020" , "data": [{"xVal": "Apr 2020", "yVal": "1"} ]}} {:result { "sectionTitle" : "B", "dataPresent":"1" , "asOfDate": "05/17/2021" , "data": [ ]}} {:result { "sectionTitle" : "C", "dataPresent":"1" , "asOfDate": "05/17/2021" , "data": [ ]}}) then I am using below function to iterate over this and update all the asOfDate fields. (map (fn [x] (update-in x [(get-in x [:result :asOfDate])] dateformatFunc)) results) // results is the above list this code is not able to get the asOfDate and failing with NPE, Is there any function which can help me achieve this or do I need to first convert the list to a different structure.
Just want to inform , the result that is being returned is from Clojure code calling a Postgres function... ({:result { "sectionTitle" : "A", "dataPresent":"1" , "asOfDate": "05/17/2021" , "actualDate": "04/14/2020" , "data": [{"xVal": "Apr 2020", "yVal": "1"} ]}} {:result { "sectionTitle" : "B", "dataPresent":"1" , "asOfDate": "05/17/2021" , "data": [ ]}} {:result { "sectionTitle" : "C", "dataPresent":"1" , "asOfDate": "05/17/2021" , "data": [ ]}}) I am also not sure what structure is this.
Well the structure is JSON inside a Clojure map. Maybe each result is big a string, and we just are not seeing the outer quotes, because you are using print
or println
instead of pr
or prn
?
In that case you need to parse the JSON. Something like data.json or jsonista.
though looking closer that's not exactly what you're trying to do either maybe?
(update-in x [:result "asOfDate"] dateformatFunc)
thanks @U90R0EPHA and @UAEH11THP for your response
Not really, I tried cheshire and json libs but I am not able to update all asOfDate fields in the collection...... If I do this I am able to read first occurrence of asOfDate and it gives correct result but I am not able to update all the asOfDate with the correct format
(-> results
first
(:result)
(json/read-str :key-fn keyword)
(:asOfDate)
)
So this shows you have all the pieces you need to get at the data. I suggest you start by just parsing the whole list.
So here are my suggestions from earlier, but updated to use your json parsing instead of js -> clj conversion (and some corrections to typos and formatting): This one should give you exactly the shape to use the function from your original question.
(defn parse-results [results]
(map (fn [result]
(assoc result :result
(-> result :result
(json/read-str :key-fn keyword))))
results))
But I would probably simplify it to using this instead; because a map with a single :result
key wrapped around each result seems like useless redundancy to me, making it more difficult to get at the real data.
(defn parse-results [results]
(map (fn [result]
(-> result :result
(json/read-str :key-fn keyword)))
results))
this function worked like a charm, I will continue with this, thanks a ton @U90R0EPHA