Skip to content

Commit

Permalink
docs: add supported json op
Browse files Browse the repository at this point in the history
  • Loading branch information
CookiePieWw committed Sep 26, 2024
1 parent 0d565b2 commit dd7fca7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
18 changes: 17 additions & 1 deletion docs/reference/sql/functions/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,23 @@ SELECT json_to_string(parse_json('{"a": 1, "b": 2}'));
* `json_get_float(json, path)` to extract a float value from a JSON value by the path, while integer and boolean values will be converted to floats.
* `json_get_string(json, path)` to extract a string value from a JSON value by the path. All valid JSON values will be converted to strings, including null values, objects and arrays.

`path` is a string that select and extract elements from a json value. Checkout [SQL/JSONPath](https://github.com/datafuselabs/jsonb?tab=readme-ov-file#sqljsonpath) for supported `path` syntax.
`path` is a string that select and extract elements from a json value. The following operators in the path are supported:

| Operator | Description | Examples |
|--------------------------|--------------------------------------------------------------|--------------------|
| `$` | The root element | `$` |
| `@` | The current element in the filter expression | `$.event?(@ == 1)` |
| `.*` | Selecting all elements in an Object | `$.*` |
| `.<name>` | Selecting element that match the name in an Object | `$.event` |
| `:<name>` | Alias of `.<name>` | `$:event` |
| `["<name>"]` | Alias of `.<name>` | `$["event"]` |
| `[*]` | Selecting all elements in an Array | `$[*]` |
| `[<pos>, ..]` | Selecting 0-based `n-th` elements in an Array | `$[1, 2]` |
| `[last - <pos>, ..]` | Selecting `n-th` element before the last element in an Array | `$[0, last - 1]` |
| `[<pos1> to <pos2>, ..]` | Selecting all elements of a range in an Array | `$[1 to last - 2]` |
| `?(<expr>)` | Selecting all elements that matched the filter expression | `$?(@.price < 10)` |

If the path is invalid, the function will return a NULL value.

```sql
SELECT json_get_int(parse_json('{"a": {"c": 3}, "b": 2}'), 'a.c');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,23 @@ SELECT json_to_string(parse_json('{"a": 1, "b": 2}'));
* `json_get_float(json, path)` 按照路径 `path` 从 JSON 中获取浮点数值。布尔值、整数值将被转换为浮点数。
* `json_get_string(json, path)` 按照路径 `path` 从 JSON 中获取字符串。所有类型的 JSON 值都将被转换为字符串,包括数组、对象和 null。

`path` 是一个用于从 JSON 值中选择和提取元素的字符串。查看 [SQL/JSONPath](https://github.com/datafuselabs/jsonb?tab=readme-ov-file#sqljsonpath) 以了解支持的 `path` 语法。
`path` 是一个用于从 JSON 值中选择和提取元素的字符串。`path` 中支持的操作符有:

| 操作符 | 描述 | 示例 |
|--------------------------|--------------------------------------------------------------|--------------------|
| `$` | 根元素 | `$` |
| `@` | 过滤表达式中的当前元素 | `$.event?(@ == 1)` |
| `.*` | 选择对象中的所有元素 | `$.*` |
| `.<name>` | 选择对象中匹配名称的元素 | `$.event` |
| `:<name>` | `.<name>` 的别名 | `$:event` |
| `["<name>"]` | `.<name>` 的别名 | `$["event"]` |
| `[*]` | 选择数组中的所有元素 | `$[*]` |
| `[<pos>, ..]` | 选择数组中基于0的第 `n` 个元素 | `$[1, 2]` |
| `[last - <pos>, ..]` | 选择数组中最后一个元素之前的第 `n` 个元素 | `$[0, last - 1]` |
| `[<pos1> to <pos2>, ..]` | 选择数组中某个范围内的所有元素 | `$[1 to last - 2]` |
| `?(<expr>)` | 选择所有匹配过滤表达式的元素 | `$?(@.price < 10)` |

如果 `path` 是无效的,函数将返回 `NULL`

```sql
SELECT json_get_int(parse_json('{"a": {"c": 3}, "b": 2}'), 'a.c');
Expand Down

0 comments on commit dd7fca7

Please sign in to comment.