druid/docs/querying/sql-json-functions.md
Gian Merlino d4967c38f8
Various documentation updates. (#13107)
* Various documentation updates.

1) Split out "data management" from "ingestion". Break it into thematic pages.

2) Move "SQL-based ingestion" into the Ingestion category. Adjust content so
   all conceptual content is in concepts.md and all syntax content is in reference.md.
   Shorten the known issues page to the most interesting ones.

3) Add SQL-based ingestion to the ingestion method comparison page. Remove the
   index task, since index_parallel is just as good when maxNumConcurrentSubTasks: 1.

4) Rename various mentions of "Druid console" to "web console".

5) Add additional information to ingestion/partitioning.md.

6) Remove a mention of Tranquility.

7) Remove a note about upgrading to Druid 0.10.1.

8) Remove no-longer-relevant task types from ingestion/tasks.md.

9) Move ingestion/native-batch-firehose.md to the hidden section. It was previously deprecated.

10) Move ingestion/native-batch-simple-task.md to the hidden section. It is still linked in some
    places, but it isn't very useful compared to index_parallel, so it shouldn't take up space
    in the sidebar.

11) Make all br tags self-closing.

12) Certain other cosmetic changes.

13) Update to node-sass 7.

* make travis use node12 for docs

Co-authored-by: Vadim Ogievetsky <vadim@ogievetsky.com>
2022-09-16 21:58:11 -07:00

4.0 KiB

id title sidebar_label
sql-json-functions SQL JSON functions JSON functions

Druid supports nested columns, which provide optimized storage and indexes for nested data structures. See Nested columns for more information.

You can use the following JSON functions to extract, transform, and create COMPLEX<json> values.

Function Notes
JSON_KEYS(expr, path) Returns an array of field names from expr at the specified path.
JSON_OBJECT(KEY expr1 VALUE expr2[, KEY expr3 VALUE expr4, ...]) Constructs a new COMPLEX<json> object. The KEY expressions must evaluate to string types. The VALUE expressions can be composed of any input type, including other COMPLEX<json> values. JSON_OBJECT can accept colon-separated key-value pairs. The following syntax is equivalent: JSON_OBJECT(expr1:expr2[, expr3:expr4, ...]).
JSON_PATHS(expr) Returns an array of all paths which refer to literal values in expr in JSONPath format.
JSON_QUERY(expr, path) Extracts a COMPLEX<json> value from expr, at the specified path.
JSON_VALUE(expr, path [RETURNING sqlType]) Extracts a literal value from expr at the specified path. If you specify RETURNING and an SQL type name (such as VARCHAR, BIGINT, DOUBLE, etc) the function plans the query using the suggested type. Otherwise, it attempts to infer the type based on the context. If it can't infer the type, it defaults to VARCHAR.
PARSE_JSON(expr) Parses expr into a COMPLEX<json> object. This operator deserializes JSON values when processing them, translating stringified JSON into a nested structure. If the input is not a VARCHAR or it is invalid JSON, this function will result in an error.
TRY_PARSE_JSON(expr) Parses expr into a COMPLEX<json> object. This operator deserializes JSON values when processing them, translating stringified JSON into a nested structure. If the input is not a VARCHAR or it is invalid JSON, this function will result in a NULL value.
TO_JSON_STRING(expr) Serializes expr into a JSON string.

JSONPath syntax

Druid supports a subset of the JSONPath syntax operators, primarily limited to extracting individual values from nested data structures.

Operator Description
$ Root element. All JSONPath expressions start with this operator.
.<name> Child element in dot notation.
['<name>'] Child element in bracket notation.
[<number>] Array index.

Consider the following example input JSON:

{"x":1, "y":[1, 2, 3]}
  • To return the entire JSON object:
    $ -> {"x":1, "y":[1, 2, 3]}
  • To return the value of the key "x":
    $.x -> 1
  • For a key that contains an array, to return the entire array:
    $['y'] -> [1, 2, 3]
  • For a key that contains an array, to return an item in the array:
    $.y[1] -> 2