21.1. JSON Data #

This section describes how to query and transform JSON data.

21.1.1. Extracting a Value from JSON Data (json_extract, json_extract_string) #

The json_extract and json_extract_string stored procedures extract values located at the specified path from JSON data. The json_extract procedure returns the JSON data type, and the json_extract_string procedure returns the VARCHAR data type.

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute one of the following commands on the Postgres Pro AXE server:

  SELECT json_extract(JSON, path);
  SELECT json_extract_string(JSON, path);

Where:

  • JSON: The JSON data from which a value must be extracted.

    You must specify a JSON string literal or a column with JSON data.

  • path: The path to the value that must be extracted.

    You can use JSONPath (e.g., $.family) or JSON Pointer (e.g., /family). A list of paths can also be specified, in which case a list of values is returned.

Example 21.1. Extracting a Value from JSON Data

JSON data in the j column of the example table:

  {
  "family": "anatidae",
  "species": ["duck", "goose", "swan", null]
  }

Extracting a field:

  SELECT json_extract(j, '$.family') FROM example;
  "anatidae"
  SELECT json_extract_string(j, '$.family') FROM example;
  anatidae

Extracting an array element:

  SELECT json_extract(j, '$.species[0]') FROM example;
  "duck"
  SELECT json_extract_string(j, '$.species[0]') FROM example;
  duck

The JSON data type uses 0-based indexing so $.species[0] returns the first element.


21.1.2. Validating JSON Data (json_valid) #

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT json_valid(JSON);

Where JSON is the JSON data that must be validated.

You must specify a JSON string literal or a column with JSON data.

Postgres Pro AXE returns true if the JSON data is valid and false otherwise.

Example 21.2. Validating JSON Data

JSON data in the j column of the example table:

  {
  "family": "anatidae",
  "species": ["duck", "goose", "swan", null]
  }

Checking valid JSON data:

  SELECT json_valid(j) FROM example;
  true

Checking invalid JSON data:

  SELECT json_valid('invalid');
  false

21.1.3. Retrieving JSON Data Type (json_type) #

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT json_type(JSON[, path]);

Where:

  • JSON: The JSON data whose type must be retrieved.

    You must specify a JSON string literal or a column with JSON data.

  • path: The path to the JSON element whose type must be retrieved.

    You can use JSONPath (e.g., $.family) or JSON Pointer (e.g., /family). A list of paths can also be specified, in which case a list of data types is returned.

    Optional parameter.

Postgres Pro AXE can return the following data types:

  • ARRAY: JSON array.

  • BIGINT: Signed integer value.

  • BOOLEAN: Boolean value.

  • DOUBLE: Floating-point number.

  • OBJECT: JSON object, set of key-value pairs.

  • UBIGINT: Unsigned integer value.

  • VARCHAR: String value.

  • NULL: NULL value.

Example 21.3. Retrieving JSON Data Type

JSON data in the j column of the example table:

  {
  "family": "anatidae",
  "species": ["duck", "goose", "swan", null]
  }

Retrieving the type of the JSON data:

  SELECT json_type(j) FROM example;
  OBJECT

Retrieving the type of a specific field:

  SELECT json_type(j, '$.family') FROM example;
  VARCHAR
  SELECT json_type(j, '$.species') FROM example;
  ARRAY

21.1.4. Retrieving JSON Keys (json_keys) #

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT json_keys(JSON[, path]);

Where:

  • JSON: The JSON data from which keys must be retrieved.

    You must specify a JSON string literal or a column with JSON data.

  • path: The path to the JSON element from which keys must be retrieved.

    You can use JSONPath (e.g., $.family) or JSON Pointer (e.g., /family). A list of paths can also be specified, in which case a list of keys for each path is returned.

    Optional parameter.

Example 21.4. Retrieving JSON Keys

JSON data in the j column of the example table:

  {
  "family": "anatidae",
  "species": ["duck", "goose", "swan", null]
  }

Retrieving JSON keys:

  SELECT json_keys(j) FROM example;
  [family, species]

21.1.5. Retrieving the JSON Structure (json_structure) #

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT json_structure(JSON);

Where JSON is the JSON data whose structure must be retrieved.

You must specify a JSON string literal or a column with JSON data.

Postgres Pro AXE will return the structure of the JSON data. If the JSON data contains an array with elements of different data types, the data type of these elements in the returned structure is JSON.

Example 21.5. Retrieving the JSON Structure

JSON data in the j column of the example table:

  {
  "family": "anatidae",
  "species": ["duck", "goose", "swan", null]
  }

Retrieving the JSON structure:

  SELECT json_structure(j) FROM example;
  {"family":"VARCHAR","species":["VARCHAR"]}

21.1.6. Checking a JSON Value (json_contains) #

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT json_contains(JSON, value);

Where:

  • JSON: The JSON data where the search must be performed.

    You must specify a JSON string literal or a column with JSON data.

  • value: The JSON value that must be searched.

    You must specify a JSON string literal, a numeric value, or a column with JSON data. String values must be wrapped in double quotes (e.g., "duck").

Postgres Pro AXE will return true if value is contained in JSON and false otherwise.

Example 21.6. Checking a JSON Value

JSON data in the j column of the example table:

  {
  "family": "anatidae",
  "species": ["duck", "goose", "swan", null]
  }

Checking if a value is contained in the JSON data:

  SELECT json_contains(j, '"duck"') FROM example;
  true
  SELECT json_contains(j, '"eagle"') FROM example;
  false

Checking if a JSON object is contained in the JSON data:

  SELECT json_contains(j, '{"family": "anatidae"}') FROM example;
  true

21.1.7. Retrieving the JSON Array Length (json_array_length) #

Required privileges: Superuser or the Postgres Pro AXE administrator. However, all roles can execute this stored procedure on data of the pg_catalog.json type.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT json_array_length(JSON[, path]);

Where:

  • JSON: The JSON data whose array length must be retrieved.

    You must specify a JSON string literal or a column with JSON data.

  • path: The path to the JSON element whose array length must be retrieved.

    You can use JSONPath (e.g., $.family) or JSON Pointer (e.g., /family). A list of paths can also be specified, in which case a list of array lengths is returned.

    Optional parameter.

Postgres Pro AXE will return the array length of the JSON data or 0 if the JSON data is not a JSON array.

Example 21.7. Retrieving the JSON Array Length

JSON data in the j column of the example table:

  {
  "family": "anatidae",
  "species": ["duck", "goose", "swan", null]
  }

Retrieving the JSON array length:

  SELECT json_array_length(j, '$.species') FROM example;
  4

21.1.8. Transforming JSON Data (json_transform, json_transform_strict) #

The json_transform and json_transform_strict stored procedures transform JSON data into the specified structure that defines target data types and nesting. The json_transform_strict procedure returns an error when data type casting fails, while the json_transform procedure returns NULL for values that cannot be cast.

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute one of the following commands on the Postgres Pro AXE server:

  SELECT json_transform(JSON, structure);
  SELECT json_transform_strict(JSON, structure);

Where:

  • JSON: The JSON data that must be transformed.

    You must specify a JSON string literal or a column with JSON data.

  • structure: The structure to which the JSON data must be transformed, specified as a JSON value. The structure must use the same format as the output of the json_structure stored procedure.

Postgres Pro AXE will return the JSON data transformed into the specified structure. If the structure contains keys that are missing from the JSON data, they are returned as NULL.

Example 21.8. Transforming JSON Data

JSON data in the j column of the example table:

  {"family": "anatidae", "species": ["duck", "goose"], "coolness": 42.42}
  {"family": "canidae", "species": ["labrador", "bulldog"], "hair": true}

Transforming JSON data:

  SELECT json_transform(j::json, '{"family": "VARCHAR", "coolness": "DOUBLE"}') FROM example;
  {'family': anatidae, 'coolness': 42.420000}
  {'family': canidae, 'coolness': NULL}

21.1.9. Aggregating Values into a JSON Array (json_group_array) #

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT json_group_array(values);

Where values are the values that must be aggregated into a JSON array.

Postgres Pro AXE will return a JSON array with all values from the aggregation.

Example 21.9. Aggregating Values into a JSON Array

Data in the v column of the example1 table:

  42
  7

Aggregating values into a JSON array:

  SELECT json_group_array(v) FROM example1;
  [42, 7]

21.1.10. Aggregating Values into a JSON Object (json_group_object) #

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT json_group_object(keys, values);

Where:

  • keys: The keys that must be aggregated into a JSON object.

  • values: The values that must be aggregated into a JSON object.

Postgres Pro AXE will return a JSON object with all key-value pairs from the aggregation.

Example 21.10. Aggregating Values into a JSON Object

Data in the example1 table:

  k       v
  duck    42
  goose   7

Aggregating values into a JSON object:

  SELECT json_group_object(k, v) FROM example1;
  {"duck":42,"goose":7}

21.1.11. Aggregating JSON Data into a Structure (json_group_structure) #

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT json_group_structure(JSON);

Where JSON is the JSON data that must be aggregated into a structure.

You must specify a JSON string literal or a column with JSON data.

Postgres Pro AXE will return a JSON object where each key from the aggregated JSON data is mapped to its data type.

Example 21.11. Aggregating JSON Data into a Structure

JSON data in the j column of the example2 table:

  {
  "family": "anatidae",
  "species": ["duck", "goose"],
  "coolness": 42.42
  }
  {
  "family": "canidae",
  "species": ["labrador", "bulldog"],
  "hair": true
  }

Aggregating JSON data into a structure:

  SELECT json_group_structure(j) FROM example2;
  {"family":"VARCHAR","species":["VARCHAR"],"coolness":"DOUBLE","hair":"BOOLEAN"}

21.1.12. Parsing and Removing Whitespace from JSON Data (json) #

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT json(JSON);

Where JSON is the JSON data or a text string that must be converted to JSON, and from which whitespace must be removed.

You must specify a JSON string literal or a column with JSON data.

Postgres Pro AXE will convert the input to JSON and return it with whitespace removed. If the input is not valid JSON, an error is returned.

Example 21.12. Parsing and Removing Whitespace from JSON Data

JSON data in the j column of the example table:

  {
  "family": "anatidae",
  "species": ["duck", "goose", "swan", null]
  }

Parsing and removing whitespace from JSON data:

  SELECT json(j) FROM example;
  {"family":"anatidae","species":["duck","goose","swan",null]}

FAQ