Skip to content

Instantly share code, notes, and snippets.

@kopasetik
Created January 21, 2016 20:45
Show Gist options
  • Save kopasetik/c73333a2d5bb11a246e2 to your computer and use it in GitHub Desktop.
Save kopasetik/c73333a2d5bb11a246e2 to your computer and use it in GitHub Desktop.
Clojure JSON destructuring
(ns clojure-noob.core
(:gen-class)
(:require [clojure.data.json :as json]
[clojure.pprint :as pp]))
(defn get-url
"get a url's content"
[url]
(-> url
slurp
json/read-str))
(defn isMovie?
; Boolean to determine whether it's a movie or not
[item]
(if (= (item "Type") "movie")
true
false))
(defn -main
"goal: read the json contents of an API endpoint"
[]
(let
[{movies "Search"} (get-url "http://omdbapi.com/?s=highlander")]
(doall (map #(pp/pprint %) (filter isMovie? movies)))))
;=> Highlander (1986)
;=> Highlander II: The Quickening (1991)
;=> Highlander: Endgame (2000)
;=> Highlander: The Final Dimension (1994)
;=> Highlander: The Source (2007)
;=> Highlander: The Search for Vengeance (2007)
;=> Highlander: The Adventure Begins (1994)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment