Fork me on GitHub
#beginners
<
2022-12-03
>
Abhi Saxena22:12:59

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.

Abhi Saxena03:12:11

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.

skylize04:12:33

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.

valerauko04:12:30

you can use get-in with a string too, like get-in x [:result "asOfDate"]

valerauko04:12:17

though looking closer that's not exactly what you're trying to do either maybe?

(update-in x [:result "asOfDate"] dateformatFunc)

Abhi Saxena00:12:50

thanks @U90R0EPHA and @UAEH11THP for your response

skylize00:12:24

Sounds like you figured it out?

Abhi Saxena00:12:21

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)
             )

skylize00:12:44

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.

skylize00:12:25

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))

Abhi Saxena04:12:47

this function worked like a charm, I will continue with this, thanks a ton @U90R0EPHA

☺️ 1