2

I have a clojure data in the form of clojure format.

[{:dt [2017 6 30], :cashflow 431782} {:dt [2018 6 30], :cashflow 452271} {:dt [2019 6 30], :cashflow 473785} {:dt [2020 6 30], :cashflow 496374}]

I want the above clojure data in the form json. from clojure side i can always use Cheshire. But I would like tto do it on the lient side in javascript

4
  • so on client/js you'd have your data/input as string ? i.e. "[{:dt [2017 6 30], :cashflow 431782} {:dt [2018... and want to parse that ? Commented Sep 8, 2017 at 12:36
  • @birdspider yes Commented Sep 8, 2017 at 12:37
  • and you want it as cljs data or explicitly as json ? Commented Sep 8, 2017 at 12:43
  • @birdspider i want to parse this cljs to json explicitly. Is there a library? Commented Sep 8, 2017 at 12:45

2 Answers 2

3

As illustrated in this question (cljs.reader/read-string) and this (clj->js) the following should do the trick:

cljs.user=> (def data-as-str 
  "[{:dt [2017 6 30], :cashflow 431782}
    {:dt [2018 6 30], :cashflow 452271}
    {:dt [2019 6 30], :cashflow 473785}
    {:dt [2020 6 30], :cashflow 496374}]")
#'cljs.user/data-as-str

cljs.user=> (cljs.reader/read-string data-as-str)
[{:dt [2017 6 30], :cashflow 431782}
 {:dt [2018 6 30], :cashflow 452271}
 {:dt [2019 6 30], :cashflow 473785}
 {:dt [2020 6 30], :cashflow 496374}]

; i have little experience in cljs but this should deliver
; a plain old js object
cljs.user=> (clj->js (cljs.reader/read-string data-as-str))
#js [#js {:dt #js [2017 6 30], :cashflow 431782}
     #js {:dt #js [2018 6 30], :cashflow 452271}
     #js {:dt #js [2019 6 30], :cashflow 473785}
     #js {:dt #js [2020 6 30], :cashflow 496374}]

from here on you could do whatever JSON.___ or other js-json lib provides

for example:

cljs.user=> (.stringify js/JSON 
              (clj->js (cljs.reader/read-string data-as-str)))
"[{\"dt\":[2017,6,30],\"cashflow\":431782},{\"dt\":[2018,6,30],\"cashflow\":452271},{\"dt\":[2019,6,30],\"cashflow\":473785},{\"dt\":[2020,6,30],\"cashflow\":496374}]"
Sign up to request clarification or add additional context in comments.

9 Comments

Hey i really appreciate your immediate response. but this you are doing from clojure side. I want to do it on client side like in javascript.
@SufiyanAnsari that is client side. cljs is clojurescript.
nope this is all clojurescript. as you question has a clojurescript tag - I assumed you have clojurescript on the client side
@SufiyanAnsari writing a reliable parser for even trivial grammars is beyond a "link"... or even the scope of an SO question. I mean you will have to start from scratch. Just use clojurescript on the client. Or serialize to JSON on the server.
@SufiyanAnsari I just spent 4 seconds on google, and found an js/edn parser - good luck :)
|
1

You can use EDN parser implemented in JavaScript: github.com/shaunxcode/jsedn.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.