Sergio
Sergio

Reputation: 1

Clojure map, how to read

I am new in clojure , and I have one problem with this map.

{:status 200, :headers {Server openresty, Date Thu, 11 Feb 2016 11:35:11 GMT, Content-Type application/json; charset=utf-8, Transfer-Encoding chunked, Connection close, X-Source back, Access-Control-Allow-Origin *, Access-Control-Allow-Credentials true, Access-Control-Allow-Methods GET, POST}, :body {"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"base":"stations","main":{"temp":278.36,"pressure":1004,"humidity":65,"temp_min":276.05,"temp_max":280.15},"visibility":10000,"wind":{"speed":2.1,"deg":230},"rain":{"1h":0.2},"clouds":{"all":0},"dt":1455190219,"sys":{"type":1,"id":5091,"message":0.0549,"country":"GB","sunrise":1455175330,"sunset":1455210486},"id":2643743,"name":"London","cod":200}, :request-time 695, :trace-redirects [http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=44db6a862fba0b067b1930da0d769e98], :orig-content-encoding nil}

I want to read fields but with get-in can not make this job.Only with

(get-in location [:body])

I can read the body of map but when I make

(get-in location ["coord" "log"]) 

I just get a nil response. How I can read this fields with clojure? Thanks and sorry for my bad english.

Upvotes: 0

Views: 326

Answers (1)

tanzoniteblack
tanzoniteblack

Reputation: 671

The body is json, so you need to first use a json library to decode the body string before being able to treat it as a map:

Using cheshire, one of the major JSON libraries for Clojure, you could write this code like:

(require '[cheshire.core])

(-> location
    :body
    cheshire.core/parse-string
    (get-in ["coord" "log"]))

Upvotes: 2

Related Questions