Search code examples
jsonpostgresqljsonbpostgresql-13

Identify json type in postgresql


Is there a function - or a way to easily determine the json data type in postgresql?

For example:

select key, value, pg_typeof(value) FROM jsonb_each('{"foo":1,"bar":"2","ack":[1,2,3]}'::jsonb)

Returns:

key value   pg_typeof
ack [1, 2, 3]   jsonb
bar "2" jsonb
foo 1   jsonb

How would I determine that the value for ack is an array, bar is a string, and foo is a number?


Solution

  • From here JSON operators, jsonb_typeof:

     select key, value, jsonb_typeof(value) FROM jsonb_each('{"foo":1,"bar":"2","ack":[1,2,3]}'::jsonb);
     key |   value   | jsonb_typeof 
    -----+-----------+--------------
     ack | [1, 2, 3] | array
     bar | "2"       | string
     foo | 1         | number