inserting JSON using HoneySQL into Postgress

147 Views Asked by At

I've got a JSON field on a Postgres DB table that I'd like to populate with some clojure object (a clojure dict/map, for example).

I'd like to use Honey SQL (a Clojure lib that offers composable syntax for creating SQL expressions). How do I go about in doing this?

1

There are 1 best solutions below

0
On

The very first problem (which is also documented in the README), is this: Honey SQL treats my Clojure map as part of its syntax, rather then as the-thing-I-want-to-insert

The recommended way to do this is to use the :float keyword to tell Honey SQL that what you're trying to insert should not be interpreted as Honey SQL's DSL.

but that didn't work - kept getting an error that suggested I should cast the result. Its likely I was doing something wrong, but I did find a workaround:

(1) convert the Clojure dict to json string using Cheshire (2) and then use Honey SQL's :cast keyword to cast the string into json:

  (let [json-as-string (cheshire.core/generate-string {:mykey :myvalue})
        values-to-inset [group-key handler-id [:cast json-as-string :json]]])