In this step, you will learn how to extract data from advanced data types in PostgreSQL, specifically focusing on JSONB and Array types. We will build upon the concepts introduced in the previous steps.
If you're not already in the PostgreSQL shell, connect to the database:
sudo -u postgres psql -d labex
First, let's create a table that includes both a JSONB column and an array column:
CREATE TABLE products (id SERIAL PRIMARY KEY, name VARCHAR(255), details JSONB, tags TEXT[]);
This command creates a table named products
with the following columns: id
(an auto-incrementing integer primary key), name
(a string), details
(a JSONB column), and tags
(an array of strings).
You should see output similar to this:
CREATE TABLE
Now, let's insert some data into the products
table:
INSERT INTO products (name, details, tags) VALUES ('Laptop', '{"brand": "Dell", "model": "XPS 13", "specs": {"ram": "16GB", "storage": "512GB SSD"}}', ARRAY['electronics', 'computers', 'portable']);
INSERT INTO products (name, details, tags) VALUES ('Keyboard', '{"brand": "Logitech", "model": "G915", "specs": {"type": "Mechanical", "backlight": "RGB"}}', ARRAY['electronics', 'accessories', 'input']);
These commands insert two rows into the products
table, including JSONB data in the details
column and array data in the tags
column.
You should see output similar to this for each insert:
INSERT 0 1
To extract data from the details
(JSONB) column, you can use the ->
and ->>
operators, as learned in Step 1. For example, to extract the brand of the first product:
SELECT details ->> 'brand' FROM products WHERE id = 1;
This command retrieves the value associated with the key brand
from the details
column of the products
table where the id
is 1.
You should see output similar to this:
?column?
----------
Dell
(1 row)
To extract nested data from the details
column, you can chain the ->
and ->>
operators. For example, to extract the RAM specification of the first product:
SELECT details -> 'specs' ->> 'ram' FROM products WHERE id = 1;
This command retrieves the value associated with the key ram
within the specs
object in the details
column.
You should see output similar to this:
?column?
----------
16GB
(1 row)
To extract data from the tags
(array) column, you can use array indexing, as learned in Step 2. For example, to extract the first tag of the first product:
SELECT tags[1] FROM products WHERE id = 1;
This command retrieves the element at index 1 from the tags
array of the products
table where the id
is 1.
You should see output similar to this:
tags
-----------
electronics
(1 row)
You can also combine these techniques to extract data from both JSONB and array columns in the same query. For example, to retrieve the name, brand, and first tag of all products:
SELECT name, details ->> 'brand', tags[1] FROM products;
This command retrieves the name
, the brand
from the details
JSONB column, and the first element of the tags
array for each product.
You should see output similar to this:
name | ?column? | tags
----------+------------+-----------
Laptop | Dell | electronics
Keyboard | Logitech | electronics
(2 rows)
Finally, let's clean up the table we created:
DROP TABLE products;
You should see output similar to this:
DROP TABLE
When you're done with all the steps, you can exit the PostgreSQL shell by typing:
\q