
JavaScript Object Notation (JSON) is a modern data exchange format often used in API-based services. JSON relies on key-value pairs that make it suitable for humans and machines to read and write. The PostgreSQL database server supports the JSON data type to store semi-structured data.
Depending on the complexity of your application, you can choose from dozens of PostgreSQL inbuilt functions and operators to manipulate JSON data.
This guide takes you through implementing the JSON data type with the PostgreSQL database on Ubuntu 20.04 server.
To proceed with this guide:
The first step in this guide is setting up a database. Then, create a sample table that implements the JSON data type in a few columns. Execute the steps below to initialize the database:
Log in to the PostgreSQL server as a postgres user.
Enter the postgres user password and press Enter to proceed. Then, create a sample online_shop database.
Output.
Connect to the new online_shop database.
Output.
Create a new customers table with five columns. Assign a unique identifier to the customers using the customer_id PRIMARY KEY. Use the SERIAL keyword to instruct PostgreSQL to automatically assign a new customer_id for each customer during the INSERT statement. Define the profile and address columns using the JSON data type. This guide later shows you how to use the two columns to store the customers' profiles and addresses using the JSON format.
Output.
Your sample database and table are now in place. Proceed to the next step and populate the table.
When inserting data to a PostgreSQL JSON column, you must:
Enclose the JSON keys in double quotes ("").
Separate the key from the value using a colon (:).
Separate key-value pairs with a comma (,).
Use the supported JSON data types: strings, numbers, JSON objects, booleans, and null.
The following sample illustrates the above JSON rules.
To fully understand the PostgreSQL JSON data type, follow the steps below to insert three sample records into the customers table. In the following SQL statements, you're inserting the customers gender, date of birth (dob), and their remaining account_credit under the JSON profile column. Then, you insert the customers' addresses such as address_line_1, address_line_2, town, state, and zip under the JSON address column.
Output.
The sample JSON data is now in place. Proceed next to work with some PostgreSQL JSON functions.
PostgreSQL supports different functions and operators that you can use to query the data. Use these functions to filter records, generate reports, and more. Run the following SELECT statements to understand how these functions and operators work:
Use the ->> operator to get a specific JSON object field. For instance, run the SQL statements below to retrieve the customers' gender from the profile column.
Output.
Use the ->> operator and the SQL WHERE clause to filter records:
Retrieve a list of all women from the customers table.
Output.
List all males (M) from the customers table.
Output.
Use the PostgreSQL math functions to compute results from JSON columns. For instance, run the SQL command below to get the total customers' account balance from the account_credits key under the profile column.
Output.
Retrieve the age of customers by computing the difference between the customers' date of birth (dob) and this year (date_part('year', NOW())).
Output.
Run the json_typeof function to get the data type of a JSON column.
Output.
Use the json_each_text function to expand a JSON column into a set of key-value pairs.
Output.
Run the json_object_keys function to return all keys from a JSON column.
Output.
This guide implements the JSON data type with the PostgreSQL database server on Ubuntu 20.04 server. Use the above JSON syntax and examples when working on your next JSON project.
For a complete list of PostgreSQL JSON functions and operators, visit the official link below:
0 Comments
Be the first to comment and share your perspective with the community.