0

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?

1 Answer 1

1

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();
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.