1.2.0 permalink Arrow_down_16x16
  • (read-json input)
  • (read-json input keywordize?)
  • (read-json input keywordize? eof-error? eof-value)
Reads one JSON value from input String or Reader.
If keywordize? is true (default), object keys will be converted to
keywords. If eof-error? is true (default), empty input will throw
an EOFException; if false EOF will return eof-value.

2 Examples top

  • (use 'clojure.contrib.json)
    (import 'java.io.FileReader)
    (clojure.contrib.json/read-json (FileReader. "somedata.json"))
  • ;;Clojure 1.2 and newer (see below)
    ;;Note that the read is done into a Clojure data structure
    user=> (read-json "{\"name\": \"Steven\"}")
    {:name "Steven"}
    user=> (class (read-json "{\"name\": \"Steven\"}"))
    clojure.lang.PersistentArrayMap
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/contrib/json.clj:189 top

(defn read-json
  "Reads one JSON value from input String or Reader.
  If keywordize? is true (default), object keys will be converted to
  keywords.  If eof-error? is true (default), empty input will throw
  an EOFException; if false EOF will return eof-value. "
  ([input]
     (read-json-from input true true nil))
  ([input keywordize?]
     (read-json-from input keywordize? true nil))
  ([input keywordize? eof-error? eof-value]
     (read-json-from input keywordize? eof-error? eof-value)))
Vars in clojure.contrib.json/read-json: defn
Used in 0 other vars

Comments top

1 comment(s) for read-json.

The read-json function has undergone a transformation between 1.1 and 1.2 beta1.

The function has moved from clojure.contrib.json.read to clojure.contrib.json.

The function also has a different default behavior in 1.2 beta1 compared to 1.1.

Clojure 1.1.0
user=> (use 'clojure.contrib.json.read)
nil
user=> (read-json "{\"name\": \"Steven\"}")
{"name" "Steven"}
user=> (get *1 "name")
"Steven"
user=>
Clojure 1.2.0-beta1
user=> (use 'clojure.contrib.json)
nil
user=> (read-json "{\"name\": \"Steven\"}")
{:name "Steven"}
user=> (get *1 "name")
nil
user=>

To get 1.1 behavior in 1.2 beta1 you have to pass false as second argument (keywordize?):

Clojure 1.2.0-beta1
user=> (use 'clojure.contrib.json)
nil
user=> (read-json "{\"name\": \"Steven\"}" false)
{"name" "Steven"}
user=> (get *1 "name")
"Steven"
user=>
Log in to add a comment.