Files
honore-beaugrand/homework - solution.ipynb
T
2018-08-19 12:36:08 -04:00

4.1 KiB

First, we need to load the cheshire module to load json.

In [1]:
(require '[cheshire.core :as cheshire])

Second, we define a variable that points to the url we wished to download

In [2]:
(def election-results-url "https://dgeq.org/doc/gen7-4-2014/resultats.json")
Out [2]:
#'user/election-results-url

Then, we download the content of the url as a "string". See the slurp function to see how it works.

In [3]:
(def raw-data (slurp election-results-url))
Out [3]:
#'user/raw-data

Now, let's print some information about the downloaded string...

We can use subs for that

In [9]:
(println "the length of the string is..." (count raw-data))
(println "the string starts with..." (subs raw-data 0 50))
the length of the string is... 193056
the string starts with... {"statistiques":{"partisPolitiques":[{"numeroParti

We can now use cheshire to load the data. The parse-string functions looks awesome to do that.

In [12]:
(def data (cheshire/parse-string raw-data true))
Out [12]:
#'user/data

We can now explore the real data structure..

In [14]:
(println "the data structure type is... " (type data))
(println "the keys are... " (keys data))
the data structure type is...  clojure.lang.PersistentArrayMap
the keys are...  (:statistiques :circonscriptions)

We can now use the clojure inverse of slurp, the spit function, to serialize the data to a file. The file should be named resources/data/election-2014.edn.

In [25]:
(spit "resources/data/election-2014.edn" (str data))
In [ ]: