Required a namespace, but it is not recognized by the compiler

I’m a beginner writing a weather app as my first little test program (the full code is below, minus the API key). When I do “lein run”, I get an error stating “no such namespace: json”. However, I explicitly required the json library, and I have it in my dependencies as folllows: :dependencies [[org.clojure/clojure "1.11.1"] [clj-http "3.11.0"] [org.clojure/data.json "2.4.0"]]. What am I missing here? I have the dependency and I’ve imported the namespace. Thanks for the help!

(ns weather.core
  (:gen-class
   :require [[clojure.data.json :as json]
             [clj-http.client :as http]]))

(defn get-weather [lat lon api-key]
  (let [url (str "http://api.openweathermap.org/data/3.0/onecall?lat=" 
                 lat "&lon=" lon "&appid=" api-key)]
    (-> (http/get url)
        :body
        (json/read-str))))

(defn -main
  "I don't do a whole lot ... yet."
  [& args]
  (println (get-weather  32.832809  -117.271271 api-key)))

I think the problem is in your dependency, it should be:
[org.clojure/data.json "2.4.0"]

Never mind, that’s what you have. I think we need to see your whole project definition. Can you share the entire project on Github? (Apart from your API key, of course, which ideally your code would obtain through an environment variable.)

Sure, here it is (minus the API key)! Let me know if there’s anything else I can help with

Awesome! I’ll take a look quickly. I was also about to post a link to where I use the exact same weather API in one of my own Clojure projects, although I use a different JSON parsing library: shade/weather.clj at main · brunchboy/shade · GitHub

1 Like

Ah, yes, figured it out, it is a couple of structural problems in your namespace declaration. Try this version:

(ns weather.core
  (:gen-class)
  (:require [clojure.data.json :as json]
            [clj-http.client :as http]))
1 Like

That works, thank you! So then clojure’s keyword arguments require parentheses to be parsed correctly?

That’s not the issue (and is not true). The ns macro is a complicated macro with its own structuring rules, you just need to invoke it correctly. You had passed your :require declaration as arguments to your :gen-class directive.

I see, thank you!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.