1
votes

I am using Atom and Leiningen to program in Clojure for a University module. I have a question that requires me to parse JSON, and I'm not really sure how to go about setting it up so that I can use JSON with Atom.

I read here that you need to add:

(ns example (:require [clojure.data.json :as json]))

To be able to use JSON in your Clojure code, however I get this error:

FileNotFoundException Could not locate clojure/data/json__init.class or clojure/data/json.clj on classpath. clojure.lang.RT.load (RT.java:456)

I assume this is something to do with not adding it as a dependency as the github link says to do so. But I am not really sure how to add a dependency to my ATOM project. Could anyone explain how I'd go about doing that?

Thanks.

2
Since you're using Leiningen I assume you have a project.clj file in your top-level project directory. That's where the dependency would go. - jas
what is the code to add a dependency if you don't mind me asking? - broliverparker
I don't mind at all, see answer below. - jas

2 Answers

1
votes

Dependencies would go into your project.clj file. E.g.:

(defproject default "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.7.0"]
                 [org.clojure/data.json "0.2.6"]])
1
votes

Importing a library in your code is not enough because your project does not know you are going to use it. In your project.clj file that is on the root level of your project folder, add a new entry into :dependencies vector as follows:

:dependencies [[org.clojure/clojure "1.7.0"]
               [org.clojure/data.json "0.2.6"]]

Now, restart the repl. The library will be downloaded and available in your code.

By the way, you may use the new project structure with Clojure CLI tools that were released with Clojure 1.9. With new structure, you put the libraries you'd like to use in a special file deps.edn:

{:deps
 {org.clojure/data.json {:mvn/version "0.2.6"}}}

Now, run cli command that will launch the repl with the json library onboard. See the documentation page for more information.