converting json object to string in clojure

1.3k Views Asked by At

I am trying to display a json object from a table to UI screen using clojure,sql korma and angularJS. I have a table with a column's data type as json.The data base is postgres.When I am trying to run the code, I am getting the an error. My code to query the DB is below.

(ns error_api_transactions.models.bre_dve_errors_api_transactions
  (:require [debug.logger :as logger])
  (:use [korma.core]
        [core.config.db]
        [utils.gen_password]
        [core.file-store]
        [utils.uuid :as utils-uuid]))


(defentity bre_errors
  (pk :id)
  (table :bre_errors)
  (database master-db))

(defentity dve_errors
  (pk :id)
  (table :dve_errors)
  (database master-db))

 (defentity vendor_detail
  (pk :id)
  (table :vendor)
  (database master-db))


    (defn get-all-bre-errors
       ^{:Comments ""}
       []
      

      
       
       (select bre_errors
                       (fields [:vendor_id :vendor_id]
                               [:error_json]
                               [:error_xml :error_xml]
                               [:input :input]
                               [:created_on :created_on]
                               [:updated_on :updated_on]
                               [:deleted_on :deleted_on]
                               [:po_number :po_number]
                               [:purchase_order_id :purchase_order_i])
                       )

      )

Error is: com.fasterxml.jackson.core.JsonGenerationException: Cannot JSON encode object of class: class org.postgresql.util.PGobject: ["Container Numbers in all nodes must be consistent"]

I do not know if we can use any toString methods of java here to enter code hereconvert JSON to normal string. Any help is highly appreciated

2

There are 2 best solutions below

0
sujoy saha On BEST ANSWER

As the datatype of :error_json column is "json" in postgres database so you have to typecast it into "string" from "PGobject json".

Yes you can use toString to change the type.

below is the code snippet:

(map (fn [value] (update-in value [:error_json] #(.toString %))) (get-all-bre-errors))  
0
Hezi On

As an alternative to modifying the application code, you can modify your SQL query, like this:

SELECT id, jsonbValue::TEXT
FROM myTable

Adding ::TEXT will cast the jsonb to text.