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

Sufiyan Ansari
  • 1,780
  • 20
  • 22

2 Answers2

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}]"
birdspider
  • 3,034
  • 1
  • 16
  • 25
  • 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. – Sufiyan Ansari Sep 08 '17 at 12:50
  • @SufiyanAnsari that *is* client side. `cljs` is clojurescript. – Jared Smith Sep 08 '17 at 12:52
  • nope this is all clojurescript. as you question has a clojurescript tag - I assumed you have clojurescript on the client side – birdspider Sep 08 '17 at 12:52
  • @SufiyanAnsari if you want to do it in vanilla *JavaScript*, you'll have to write a custom parsing library. – Jared Smith Sep 08 '17 at 12:53
  • Thank you ver much @JaredSmith and birdspider. Can you please provide me a link from where i can begin with. – Sufiyan Ansari Sep 08 '17 at 13:08
  • @SufiyanAnsari `from where i can begin with` - what do you mean ? – birdspider Sep 08 '17 at 13:13
  • I mean do you have any solution or someone had already done it. Which can help me out to parse the same – Sufiyan Ansari Sep 08 '17 at 13:25
  • 1
    @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. – Jared Smith Sep 08 '17 at 13:25
  • 1
    @SufiyanAnsari I just spent 4 seconds on google, and found an js/edn parser - good luck :) – birdspider Sep 08 '17 at 13:27
1

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

Piotrek Bzdyl
  • 12,965
  • 1
  • 31
  • 49