Skip to main content
Documents
Basic SyntaxMath FunctionsDate FunctionsJSON FunctionsDatabasesTables & Schema ManagementString FunctionsTriggersIndexes

PostgreSQL GIN Indexing

PostgreSQL JSON functions allow you to store, query, and manipulate JSON data within your database. They are essential for handling JSON data types and performing operations like extraction, aggregation, and transformation.

Usage

JSON functions are used when dealing with JSON data stored in PostgreSQL, enabling efficient querying and manipulation of this semi-structured data type. They facilitate operations such as retrieving JSON objects, accessing specific values, and performing modifications.

Syntax


json -> key
jsonb ->> key
jsonb_set(target jsonb, path text[], new_value jsonb)

In these examples, `->` is used to extract JSON objects, `->>` retrieves JSON values as text, and `jsonb_set` updates JSON data at a specified path.

Difference Between `json` and `jsonb`:

  • json: Stores JSON data as text, preserving whitespace and order of keys.
  • jsonb: Stores JSON data in a binary format, offering better performance and indexing capabilities. This is why `jsonb` is generally preferred for most use cases.

Examples

1. Extracting JSON Object


SELECT data -> 'name' AS name
FROM json_table;

This query extracts the JSON object associated with the key `name` from a column `data` in the `json_table`.

2. Accessing JSON Value as Text


SELECT data ->> 'age' AS age
FROM json_table;

Here, the JSON value of `age` is retrieved as text, ideal for cases where you need the data in text format for further processing.

3. Updating JSON Data


UPDATE json_table
SET data = jsonb_set(data, '{address, city}', '"New York"')
WHERE id = 1;

This updates the `city` field within the `address` JSON object to "New York" for a specific row where `id` is 1.

4. Handling JSON Arrays


SELECT jsonb_array_elements(data -> 'tags') AS tag
FROM json_table;

This query demonstrates how to handle JSON arrays by extracting each element from the `tags` array within a JSON object.

5. JSON Operations


SELECT jsonb_each(data)
FROM json_table;

This example uses `jsonb_each` to expand the JSON object into a set of key-value pairs, useful for iterating over data.

Tips and Best Practices

  • Use `jsonb` over `json`. `jsonb` is generally preferred for better performance and indexing capabilities.
  • Index JSON data. Utilize GIN indexing for efficient querying of JSONB data to improve performance.
  • 
    CREATE INDEX jsonb_idx ON json_table USING GIN (data);
    
  • Limit JSON depth. Keep JSON structures shallow to avoid complex queries and improve readability.
  • Leverage PostgreSQL functions. Use built-in functions like `jsonb_array_elements` for handling JSON arrays effectively.
  • Validate JSON structure. Ensure JSON data adheres to expected schemas to prevent errors during querying or manipulation.
  • Handle Missing Keys Gracefully. Use functions like `coalesce` to manage missing keys and provide default values.