简体   繁体   中英

Convert jsonb postgres column data to String

I have a column of jsonb in Postgres.

Which contains data like "{\" name\":\"xyz\"}" . I want to convert it as {"name":"xyz"} . How can I achieve this?

Could you please clarify what exactly you want to do?
If you need to remove whitespaces in Java you can try something like this .

        try (final Connection connection = embeddedPostgres.getTestDatabase().getConnection();
         final Statement statement = connection.createStatement();
         final PreparedStatement updateStatement = connection.prepareStatement("update your_table set your_column = ? where id = ?")) {
        connection.setAutoCommit(false);
        try (final ResultSet resultSet = statement.executeQuery("select * from your_table order by id limit 10")) {
            while (resultSet.next()) {
                final long id = resultSet.getLong("id");
                final String infoAsString = resultSet.getString("info");

                final String withoutWhitespaces = StringUtils.deleteWhitespace(infoAsString);
                final PGobject fixedInfoObject = new PGobject();
                fixedInfoObject.setType("jsonb");
                fixedInfoObject.setValue(withoutWhitespaces);
                updateStatement.setObject(1, fixedInfoObject);
                updateStatement.setLong(2, id);
                updateStatement.executeUpdate();
            }
        }
        connection.commit();
    }

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