0

I have a single string like with multiples json:

{"operation": {"status": true, "limit": 100}}{"operation1": {"customer": "Jhon", "sum": 20, "time": "2019-02-13T10:00:00.000Z"}}{"operation1": {"customer": "Henry", "sum": 90, "time": "2019-02-13T11:00:00.000Z"}}

I would like to get a list of json to be able to process each json as an individual object.

1
  • Please add what you have tried and how it failed (e.g. exceptions). So we can improve on it. Commented Nov 28, 2019 at 10:41

3 Answers 3

3

You can use json library with streaming support. E.g.

(require '[cheshire.core :as json])

(->> "{\"operation\": {\"status\": true, \"limit\": 100}}{\"operation1\": {\"customer\": \"Jhon\", \"sum\": 20, \"time\": \"2019-02-13T10:00:00.000Z\"}}{\"operation1\": {\"customer\": \"Henry\", \"sum\": 90, \"time\": \"2019-02-13T11:00:00.000Z\"}}"
     char-array
     io/reader
     json/parsed-seq
     (take 2))

returns

({"operation" {"status" true, "limit" 100}} 
 {"operation1" {"customer" "Jhon", "sum" 20, "time" "2019-02-13T10:00:00.000Z"}})
Sign up to request clarification or add additional context in comments.

Comments

1

I had to clean up your data. You may need to clarify where it is coming from so your question is more understandable (in context).

(ns tst.demo.core
  (:use demo.core tupelo.core tupelo.test)
  (:require
    [tupelo.core :as t]
    [tupelo.string :as ts]))

; NOTE:  I added square-brackets to wrap everyting in an array, and I also
; had to add commas between the array entries
(def data
  "[ {'operation': {'status': true, 'limit': 100}},
     {'operation1': {'customer': 'Jhon', 'sum': 20, 'time': '2019-02-13T10:00:00.000Z'}},
     {'operation1': {'customer': 'Henry', 'sum': 90, 'time': '2019-02-13T11:00:00.000Z'}} ] " )

with result

(t/json->edn (ts/quotes->double data)) => 
  ({:operation {:status true, :limit 100}}
   {:operation1
     {:customer "Jhon", :sum 20, :time "2019-02-13T10:00:00.000Z"}}
   {:operation1
     {:customer "Henry", :sum 90, :time "2019-02-13T11:00:00.000Z"}})

Comments

0

This does not deserve to count as an answer beyond the helpful one by @rmcv, but it is too much for just a comment. If useful, @rmcv should feel free to add this context to their answer and I can then delete this answer...

If you want to return the JSON keywords as Clojure keywords then a tweak to that answer is:

;; Emits map with JSON keys as Clojure keywords...
(-> "{\"operation\": {\"status\": true, \"limit\": 100}}{\"operation1\":      {\"customer\": \"Jhon\", \"sum\": 20, \"time\": \"2019-02-13T10:00:00.000Z\"}{\"operation1\": {\"customer\": \"Henry\", \"sum\": 90, \"time\": \"2019-02-13T11:00:00.000Z\"}}"
    char-array
    io/reader
    (json/parsed-seq true)
    (->> (take 2)))

which returns

({:operation {:status true, :limit 100}} 
 {:operation1 {:customer "Jhon", :sum 20, :time "2019-02-13T10:00:00.000Z"}})

The true argument to parsed-seq just applies this function to the JSON keywords

(fn [k] (keyword k))

and you can do more complex things with the keyword processing this way, i.e. (not so complex):

(-> "{\"operation\": {\"status\": true, \"limit\": 100}}{\"operation1\": {\"customer\": \"Jhon\", \"sum\": 20, \"time\": \"2019-02-13T10:00:00.000Z\"}}{\"operation1\": {\"customer\": \"Henry\", \"sum\": 90, \"time\": \"2019-02-13T11:00:00.000Z\"}}"
    char-array
    io/reader
    (chesire/parsed-seq (fn [k] (keyword (clojure.string/upper-case k))))
    (->> (take 2)))

returns

({:OPERATION {:STATUS true, :LIMIT 100}} 
 {:OPERATION1 {:CUSTOMER "Jhon", :SUM 20, :TIME "2019-02-13T10:00:00.000Z"}})

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.