Search code examples
clojure

Clojure's Cheshire JSON API not reading entire JSON file


I have JSON data in the following format:

{"load":{"meta": 12345}}
{"load":{"meta": 54321}}
...

When I attempt to load the data with Cheshire I get back only the first line of data translated into edn:

(def read-json-data (parse-string (slurp "data/json_data") true))

=> {:load {:meta 12345}}

Solution

  • This is correct behaviour - cheshire/parse-string parses the first JSON object it can find.

    If you want the whole file to be parsed as a single JSON object you should make an array:

    [{"load": {"meta": 12345}},
     {"load": {"meta": 54321}},
     ...]
    

    Alternatively, if you want to parse each line separately you can do something like this:

    (map #(cheshire/parse-string % true)
         (line-seq (clojure.java.io/reader "data/json_data")))
    

    (Also, notice the colons in the JSON.)