简体   繁体   中英

converting json object to string in clojure

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 here convert JSON to normal string. Any help is highly appreciated

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))  

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM