PostgreSQL json_object_keys() Function

The PostgreSQL json_object_keys() function returns a set of keys in the specified top-level JSON object.

json_object_keys() Syntax

This is the syntax of the PostgreSQL json_object_keys() function:

json_object_keys(any_object JSON) -> SETOF TEXT

Parameters

any_object

Required. A JSON object.

Return value

The PostgreSQL json_object_keys() function returns a set ( SETOF) of text that contains all the top-level keys in the specified JSON object.

json_object_keys() Examples

This example shows how to use the PostgreSQL json_object_keys() function to get all the top-level keys of a JSON object.

SELECT json_object_keys('{"name": "Tom", "age": 20, "hobbies": ["sports", "cars"]}');
 json_object_keys
------------------
 name
 age
 hobbies

Since the json_object_keys() function return value is of type SETOF, you can use json_object_keys() in the SELECT * FROM statement:

SELECT
    *
FROM
    json_object_keys('{"name": "Tom", "age": 20, "hobbies": ["sports", "cars"]}')
    AS x(keys);
  keys
---------
 name
 age
 hobbies