Postgres jsonb to integer

When converting a JSONB value to an integer in PostgreSQL, you can use the ::int operator. This operator extracts the inner integer value from the JSONB and converts it to the integer data type. Here’s an example:

    
      SELECT '{"age": 25}'::jsonb -> 'age'::text::int;
    
  

In the above example, we have a JSONB value {"age": 25}. By using the -> operator, we extract the value for the key 'age'. The ::text operator converts this value to a text data type, and then ::int converts it to an integer.

The result of the above query will be:

    
      25
    
  

So, the JSONB value {"age": 25} is converted to the integer value 25 using the ::int operator.

Leave a comment