Search code examples
clojuretype-conversionhugsql

PGobject type conversion in Clojure


I'm having trouble with a type conversion problem in Clojure, using Hugsql. I'm new to Clojure and newer to SQL, and I'd appreciate any help I can get.

We recently migrated our PostgreSQL db so one column is a json array instead of a string -- the migration looks like:

ALTER TABLE customers ALTER id TYPE JSON USING json_build_array(id);

But now when I make query for that data, the type is a PGobject and not a collection like I was hoping. While I'm looking for:

  • ["id-123"]

the actual returned value is

  • #object[org.postgresql.util.PGobject 0x7ff0434e "[\"id-123\"]"]

I spent some time looking up how to do the type conversion, but had no luck. How can I convert the type of this response to a collection or sequence in Clojure? Thanks for your help!


Solution

  • It turns out that this is much easier than I was making it out to be.

    You can just use (.getValue my-pgobject) and it will be converted to a string like:

    "[\"id-123\"]", which can then be parsed with something like cheshire.core/parse-string