top of page
Writer's pictureThe Tech Platform

PostgreSQL JSON Functions

PostgreSQL is an open-source, powerful, and feature-rich relational database management system (RDBMS) that is designed to handle large amounts of data. It is commonly known as Postgres and is often used as a back-end database for web applications and other software.


One of the key features of PostgreSQL is its support for ACID (Atomicity, Consistency, Isolation, Durability) transactions, which ensures that data is always in a consistent state and that transactions are atomic, isolated, and durable. It also supports multi-version concurrency control (MVCC) which enables multiple transactions to work on the same data simultaneously without conflicts.


PostgreSQL is a cross-platform database and runs on Windows, Linux, and macOS. It is widely used in various industries, including finance, healthcare, e-commerce, and government.


PostgreSQL JSON Function

PostgreSQL provides several built-in functions for working with JSON data. Here are some of the most commonly used ones:

  1. json_array_length(json): This function returns the number of elements in the outermost JSON array.

  2. json_object_keys(json): This function returns an array of keys in the outermost JSON object.

  3. json_extract_path(json, path): This function returns the value at the specified path in the JSON data.

  4. json_array_elements(json): This function returns a set of JSON values for each element in the outermost JSON array.

  5. json_object_field(json, field): This function returns the value of the specified field in the outermost JSON object.

  6. json_build_object(name, value [, name, value] ...): This function creates a JSON object using the supplied key-value pairs.

  7. json_build_array(value [, value] ...): This function creates a JSON array using the supplied values.

  8. jsonb_array_length(jsonb): Same as json_array_length but for jsonb data types.

  9. jsonb_object_keys(jsonb): Same as json_object_keys but for jsonb data types.

  10. jsonb_extract_path(jsonb, path): Same as json_extract_path but for jsonb data types.

These functions allow you to easily extract and manipulate JSON data stored in PostgreSQL.


1. json_array_length(json)

You can use the jsonb_array_length() function to find the length of a JSON array that is stored in a jsonb (binary json) column. Here is an example of how you can use this function:

SELECT jsonb_array_length(json_column) FROM table_name;

This query will return the length of the JSON array stored in the column json_column of the table table_name.


You can also use the json_array_length() function which works the same as jsonb_array_length() but it will work on json type rather than jsonb.

SELECT json_array_length(json_column) FROM table_name;

You should make sure that the JSON column type is JSON or jsonb before using the function, if it's not you should cast it to JSON or jsonb type.


2. json_object_keys(json)

You can use the jsonb_object_keys() function to find the keys of a JSON object that is stored in a jsonb (binary json) column. Here is an example of how you can use this function:

SELECT jsonb_object_keys(json_column) FROM table_name;

This query will return an array of all the keys of the JSON object stored in the column json_column of the table table_name.


You can also use the json_object_keys() function which works the same as jsonb_object_keys() but it will work on JSON type rather than jsonb.

SELECT json_object_keys(json_column) FROM table_name;

The returned array will be sorted in the order of keys. Also, you should make sure that the JSON column type is JSON or jsonb before using the function, if it's not you should cast it to JSON or jsonb type.


3. json_extract_path(json, path)

You can use the jsonb_extract_path() function to extract a specific value from a JSON object that is stored in a jsonb (binary json) column. The function takes two parameters:

  • json: the jsonb column that contains the JSON object

  • path: the path to the value you want to extract, specified as a string of keys separated by periods (e.g., "key1.key2.key3").

Here is an example of how you can use this function:

SELECT jsonb_extract_path(json_column, 'key1.key2') FROM table_name;

This query will return the value at the path 'key1.key2' of the JSON object stored in the column json_column of the table table_name.


You can also use the json_extract_path() function which works the same as jsonb_extract_path() but it will work on JSON type rather than jsonb.

SELECT json_extract_path(json_column, 'key1.key2') FROM table_name;

If the path doesn't exist, the function will return NULL, also if you want to extract a key from a JSON array you should use the jsonb_array_elements() or json_array_elements() function in conjunction with the jsonb_extract_path() or json_extract_path() function accordingly.


4. json_array_elements(json)

You can use the jsonb_array_elements() function to extract the elements of a JSON array that is stored in a jsonb (binary json) column. The function takes a single parameter:

  • json: the jsonb column that contains the JSON array

Here is an example of how you can use this function:

SELECT jsonb_array_elements(json_column) FROM table_name;

This query will return all elements of the JSON array stored in the column json_column of the table table_name.


You can also use the json_array_elements() function which works the same as jsonb_array_elements() but it will work on json type rather than jsonb.

SELECT json_array_elements(json_column) FROM table_name;

It's worth noting that this function will return one row for each element of the JSON array and each row will contain a single value.


You can also use the jsonb_array_elements_text() or json_array_elements_text() function to return the elements of a JSON array as text.

SELECT jsonb_array_elements_text(json_column) FROM table_name;

or

SELECT json_array_elements_text(json_column) FROM table_name;

and also you should make sure that the JSON column type is JSON or jsonb before using the function, if it's not you should cast it to JSON or jsonb type.


5. json_object_field(json, field)

You can use the jsonb_object_field() function to extract the value of a specific field of a JSON object that is stored in a jsonb (binary json) column. The function takes two parameters:

  • json: the jsonb column that contains the JSON object

  • field: the field you want to extract, specified as a string

Here is an example of how you can use this function:

SELECT jsonb_object_field(json_column, 'field_name') FROM table_name;

This query will return the value of the field named 'field_name' of the JSON object stored in the column json_column of the table table_name.


You can also use the json_object_field() function which works the same as jsonb_object_field() but it will work on json type rather than jsonb.

SELECT json_object_field(json_column, 'field_name') FROM table_name;

If the field doesn't exist, the function will return NULL. Also, you should make sure that the JSON column type is JSON or jsonb before using the function, if it's not you should cast it to JSON or jsonb type.


6. json_build_object(name, value [, name, value] ...)

You can use the jsonb_build_object() function to create a JSON object from the given key-value pairs. The function takes an even number of arguments, where each pair of arguments represents a key-value pair. The first argument of each pair is the key, specified as a string, and the second argument is the value, which can be of any data type.


Here is an example of how you can use this function:

SELECT jsonb_build_object('field1', 'value1', 'field2', 'value2')

This query will return a JSON object with two fields, 'field1' and 'field2', with the corresponding values 'value1' and 'value2'.


You can also use the json_build_object() function which works the same as jsonb_build_object() but it will work on json type rather than jsonb.

SELECT json_build_object('field1', 'value1', 'field2', 'value2')

You can also use the function in a SQL query to insert data into a table, like this:

INSERT INTO table_name (json_column) VALUES (jsonb_build_object('field1', 'value1', 'field2', 'value2'));

or

INSERT INTO table_name (json_column) VALUES (json_build_object('field1', 'value1', 'field2', 'value2'));

The function can take any number of key-value pairs, so you can create JSON objects with any number of fields.


7. json_build_array(value [, value] ...)

You can use the jsonb_build_array() function to create a JSON array from a list of values. The function takes one or more arguments, where each argument represents a value in the array. The values can be of any data type.


Here is an example of how you can use this function:

SELECT jsonb_build_array('value1', 'value2', 'value3');

This query will return a JSON array containing the values 'value1', 'value2', 'value3'.


You can also use the json_build_array() function which works the same as jsonb_build_array() but it will work on json type rather than jsonb.

SELECT json_build_array('value1', 'value2', 'value3');

You can also use the function in a SQL query to insert data into a table, like this:

INSERT INTO table_name (json_column) VALUES (jsonb_build_array('value1', 'value2', 'value3'));

or

INSERT INTO table_name (json_column) VALUES (json_build_array('value1', 'value2', 'value3'));

The function can take any number of values, so you can create JSON arrays of any size. Also, you should make sure that the JSON column type is JSON or jsonb before using the function, if it's not you should cast it to JSON or jsonb type.


Conclusion:

PostgreSQL is known for its reliability, data integrity, and robustness. It supports advanced data types, such as arrays, hstore, JSON, and JSONB, and can handle complex queries with ease. It is also highly extensible and allows users to define custom data types, operators, and functions.

0 comments

Kommentare


bottom of page