How to detect JSON column type from Database
<p>Have some code that transforms a database ResultSet into JSON.  Right now, it handles numeric values and Strings, and works just fine and dandy.  However, when it gets a JSON value, it uses <code>toString</code> on it, instead of treating it as JSON, which means when extracted, it has escaped double quotes, etc., and cannot be re-serialized into JSON.</p>

<p>How can I detect a JSON column type from a PostgreSQL database?</p>

<p>EDIT: Here's the solution, as suggested by @f1sh.  @f1sh, if you want to submit your comment as an answer, I'll mark it as the accepted solution.</p>

<pre><code>private JsonNode resultSetToJson(ResultSet rs) throws SQLException {

    ObjectNode node = DataFileTool.OBJECT_MAPPER.createObjectNode();

    ResultSetMetaData mData = rs.getMetaData();
    int columnCount = mData.getColumnCount();

    for (int i = 1; i &lt;= columnCount; i++) {

        Object obj = rs.getObject(i);
        String columnName = mData.getColumnLabel(i);
        String columnNameToUse = dbColumnNameToJsonKey.containsKey(columnName)
                ? dbColumnNameToJsonKey.get(columnName) : columnName;

        if (mData.getColumnType(i) == Types.NUMERIC) {
            if (obj == null) {
                node.putNull(columnNameToUse);
            } else if (obj instanceof BigDecimal) {
                node.put(columnNameToUse, (BigDecimal) obj);
            } else if (obj instanceof Integer) {
                node.put(columnNameToUse, (Integer) obj);
            } else if (obj instanceof Double) {
                node.put(columnNameToUse, (Double) obj);
            } else if (obj instanceof Long) {
                node.put(columnNameToUse, (Long) obj);
            } else if (obj instanceof Float) {
                node.put(columnNameToUse, (Float) obj);
            } else if (obj instanceof Short) {
                node.put(columnNameToUse, (Short) obj);
            } else if (obj instanceof BigInteger) {
                node.set(columnNameToUse, BigIntegerNode.valueOf((BigInteger) obj));
            } else if (obj instanceof Byte) {
                node.put(columnNameToUse, (Byte) obj);
            } else {
                node.put(columnNameToUse, obj.toString());
            }
        } else if (obj instanceof PGobject) {
            PGobject pg = (PGobject) obj;

            if (pg.getType().equalsIgnoreCase("json")) {
                JsonNode pgNode;

                try {
                    pgNode = DataFileTool.OBJECT_MAPPER.readTree(pg.getValue());
                    node.set(columnNameToUse, pgNode);
                } catch (IOException e) {
                    LOGGER.error("An exception occurred while attempting to parse JSON PGObject to JsonNode: {}",
                            obj, e);
                    node.put(columnNameToUse, obj.toString());
                }
            } else {
                node.put(columnNameToUse, obj.toString());
            }
        } else if (obj == null) {
            node.put(columnNameToUse, StringUtils.EMPTY);
        } else {
            node.put(columnNameToUse, obj.toString());
        }
    }

    return node;
}
</code></pre>