21.1. Извлечение значения из JSON-данных (json_extract, json_extract_string) #
Хранимые процедуры json_extract и json_extract_string извлекают значения по указанному пути из JSON-данных. Процедура json_extract возвращает тип данных JSON, а процедура json_extract_string возвращает тип данных VARCHAR.
Требуемые права: только администратор Postgres Pro AXE. За полным списком хранимых процедур и прав обратитесь к Разделу 12.1.
Выполните одну из следующих команд на сервере Postgres Pro AXE:
SELECT json_extract(JSON,путь);
SELECT json_extract_string(JSON,путь);
Где:
JSON: JSON-данные, из которых требуется извлечь значение.Необходимо указать строковой литерал JSON или столбец с JSON-данными.
путь: путь к значению, которое требуется извлечь.Можно использовать JSONPath (например,
$.family) или JSON Pointer (например,/family). Также можно указать список путей — в этом случае возвращается список значений.
Postgres Pro AXE возвращает извлечённые значения.
Пример 21.1. Выполнение хранимых процедур json_extract и json_extract_string
JSON-данные в столбце j таблицы example:
{
"family": "anatidae",
"species": ["duck", "goose", "swan", null]
}Извлечение поля:
SELECT json_extract(j, '$.family') FROM example;
"anatidae"
SELECT json_extract_string(j, '$.family') FROM example;
anatidae
Извлечение элемента массива:
SELECT json_extract(j, '$.species[0]') FROM example;
"duck"
SELECT json_extract_string(j, '$.species[0]') FROM example;
duck
Тип данных JSON использует индексацию с 0, поэтому $.species[0] возвращает первый элемент.
21.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: Postgres Pro AXE administrator only. For a full list of stored procedures and privileges, refer to Section 12.1.
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.
Postgres Pro AXE returns the extracted values.
Example 21.1. Executing the json_extract and json_extract_string Stored Procedures
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.