I am trying to pretty print a JSON from clojurescript, on the browser's console.
I found the following link - How can I pretty-print JSON using JavaScript?
The above link provides the following js - JSON.stringify(obj, undefined, 2)
The following translation in cljs doesnt work (.stringify js/JSON obj undefined 2)
UPDATE: ClojureScript now has a full port of clojure.pprint in the form of cljs.pprint.
There's also a fipp which is narrower in scope and likely a bit faster.
cljs.user> (.stringify js/JSON (clj->js {:foo 42}) nil 2)
"{\n \"foo\": 42\n}"
cljs.user> (pr-str {:foo 42})
"{:foo 42}"
The following converts a Clojure map (object
) to JSON and prints it in the console as an object that can hence leverage the browsers inspect JSON functionality:
(.dir js/console (clj->js object))
EDIT: While pretty printing is really nice, in the developer console I still prefer the ability to browse the data structure as a tree and now frequently use cljs-devtools. It is a library that gives you an interactive data tree that can be expanded as a normal js-object but for vanilla clojure without having to convert to js, meaning :keywords
, {:ma "ps"}
and the rest of the clj-family.
At the moment it requires you to add a leiningen dependency and some code to your project and to use Chrome Canary.
clojure.pprint
has been ported to ClojureScript with release 0.0-3255
. It is called cljs.pprint
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With