PostgreSQL JSON
Summary: in this tutorial, you will learn about JSON and how to work with JSON data in PostgreSQL using the PostgreSQL JSON and JSONB data types.
What is JSON
JSON stands for JavaScript Object Notation. JSON is a lightweight data interchange format that is readable for humans and simple for computers to parse.
JSON is based on two main data structures objects and arrays:
Objects
An object is defined as an unordered collection of key-value pairs enclosed in curly braces {}
. Each pair includes:
- A key which is a string surrounded by double quotes (“).
- A colon
:
that separates the key and value. - A value that can be a string, a number, or even an object.
For example, the following illustrates a JSON object that represents a film:
The film object has three keys title
, release_year
, and length
with associated values.
Arrays
An array is an ordered list of values enclosed in square brackets []
. The values do not have to be the same type. Additionally, an array may contain values of any valid JSON data type including objects and arrays.
For example, the following shows an array that stores three film titles as strings:
Data types in JSON
JSON supports some data types including:
- String: “Joe”
- Number: 100, 9.99, …
- Boolean: true and false.
- Null: null
JSON data can be particularly useful for creating configuration files or exchanging data between a client and a server.
PostgreSQL JSON data types
PostgreSQL offers two data types for storing JSON:
- JSON – store an exact copy of the JSON text.
- JSONB – store the JSON data in binary format.
JSON vs. JSONB
The following table illustrates the key differences between JSON and JSONB types:
Feature | JSON | JSONB |
---|---|---|
Storage | Textual representation (verbatim) | Binary storage format |
Size | Typically larger because it retains the whitespace in JSON data | Typically smaller |
Indexing | Full-text search indexes | Binary indexes |
Performance | Slightly slower | Generally faster |
Query performance | Slower due to parsing | Faster due to binary storage |
Parsing | Parse each time | Parse once, store in binary format |
Data manipulation | Simple and easy | More complex |
Ordering of keys | Preserved | Not preserved |
Duplicate keys | Allow duplicate key, the last value is retained | Do not allow duplicate keys. |
Use cases | Storing configuration data, log data, simple JSON documents | Storing JSON documents where fast querying and indexing are required |
In practice, you should use JSONB to store JSON data unless you have specialized requirements such as retaining the ordering of keys in the JSON documents.
PostgreSQL JSON examples
Let’s take some examples of storing JSON data in the PostgreSQL database.
1) Storing JSON objects example
First, create a new table called products
:
The products
table includes three columns:
- The
id
column is the primary key column that uniquely identifies the product. - The
name
column stores the product name. - The
properties
column stores various properties of a product such as size, and color.
Second, insert JSON data into the products
table:
Output:
Third, insert multiple rows into the products
table:
Output:
Fourth, retrieve JSON data from the products
table:
Output:
Fifth, retrieve the product with the colors extracted from the JSON data in the properties
column:
Output:
The ->
operator extracts a JSON object field by a key. In this example, we use the -> operator ->
to extract the color of the properties object:
The values in the color column are surrounded by double quotes (“).
To extract a JSON object field by a key as text, you can use the ->>
operator. For example:
Output:
Sixth, retrieve the products with the colors black and white using the ->>
operator in the WHERE clause:
Output:
2) Storing JSON arrays example
First, create a table called contacts
for storing the contact persons:
The phones
column has the JSONB data type. We’ll store both work phone and personal number numbers in a JSON array in the phones
column.
Second, insert new rows into the contacts
table:
Output:
Third, retrieve the contacts with the work phone numbers from the contacts
table:
Output:
The ->> index
extract the index element in an array. In this example, we use the ->> 0
to extract the first elements in the phones
array as text.
Check JSON functions to process JSON data.
Summary
- JSON stands for JavaScript Object Notation.
- Use JSONB data type to store JSON data.