| 1 | <!-- groonga-command --> |
| 2 | <!-- database: functions_json_extract --> |
| 3 | |
| 4 | # `json_extract` |
| 5 | |
| 6 | ```{versionadded} 16.0.6 |
| 7 | |
| 8 | ``` |
| 9 | |
| 10 | ```{note} |
| 11 | This is an experimental feature. Currently, this feature is still not stable. |
| 12 | ``` |
| 13 | |
| 14 | ## Summary |
| 15 | |
| 16 | `json_extract` extracts values from a JSON by a JSONPath expression. |
| 17 | |
| 18 | The extracted values keep their JSON types. For example, a string in |
| 19 | JSON is extracted as a text and an integer in JSON is extracted as an |
| 20 | integer. So you can use `json_extract` for both full text search |
| 21 | against strings and range search against numbers. |
| 22 | |
| 23 | `json_extract` is often used in combination with the {ref}`select-filter` |
| 24 | option of {doc}`/reference/commands/select`. For example, you can |
| 25 | combine `json_extract` with {doc}`between` to do a range search against |
| 26 | values that are stored in a JSON. |
| 27 | |
| 28 | ```{note} |
| 29 | `json_extract` requires the |
| 30 | [simdjson](https://github.com/simdjson/simdjson) and |
| 31 | [jsoncons](https://danielaparker.github.io/jsoncons/) libraries. It |
| 32 | isn't available if Groonga is built without them. |
| 33 | ``` |
| 34 | |
| 35 | ## Syntax |
| 36 | |
| 37 | `json_extract` requires two parameters: |
| 38 | |
| 39 | ``` |
| 40 | json_extract(json, jsonpath) |
| 41 | ``` |
| 42 | |
| 43 | ## Usage |
| 44 | |
| 45 | Here are a schema definition and sample data to show usage. The `value` |
| 46 | column uses the `JSON` type and stores nested arrays: |
| 47 | |
| 48 | <!-- groonga-command --> |
| 49 | |
| 50 | ```{include} ../../example/reference/functions/json_extract/usage_setup.md |
| 51 | table_create Data TABLE_NO_KEY |
| 52 | column_create Data value COLUMN_SCALAR JSON |
| 53 | |
| 54 | load --table Data |
| 55 | [ |
| 56 | {"value": "{\"value\": [[1, 10], [100]]}"}, |
| 57 | {"value": "{\"value\": [[2], [20, 200]]}"}, |
| 58 | {"value": "{\"value\": [[-1, -10], [-100]]}"} |
| 59 | ] |
| 60 | ``` |
| 61 | |
| 62 | The following example extracts all elements of the nested arrays by the |
| 63 | `$.value[*][*]` JSONPath and selects the records that have a value |
| 64 | between `10` and `20`: |
| 65 | |
| 66 | <!-- groonga-command --> |
| 67 | |
| 68 | ```{include} ../../example/reference/functions/json_extract/usage_select.md |
| 69 | select Data --filter 'between(json_extract(value, "$.value[*][*]"), 10, 20)' |
| 70 | ``` |
| 71 | |
| 72 | The records whose extracted values are between `10` and `20` are |
| 73 | selected even though the values are stored in nested arrays in JSON. |
| 74 | |
| 75 | ## Parameters |
| 76 | |
| 77 | There are two required parameters. |
| 78 | |
| 79 | ### Required parameters |
| 80 | |
| 81 | #### `json` |
| 82 | |
| 83 | `json` is the target JSON. It must be a parsed JSON, such as a value of |
| 84 | a `JSON` typed column, or a JSON string. |
| 85 | |
| 86 | `json_extract` reports an error if this parameter isn't a parsed JSON |
| 87 | nor a JSON string. |
| 88 | |
| 89 | #### `jsonpath` |
| 90 | |
| 91 | `jsonpath` is a JSONPath expression that matches the values to extract. |
| 92 | |
| 93 | A JSONPath expression begins with `$`, which means the root of the JSON |
| 94 | value. For example, `$.value[*]` matches all elements of the `value` |
| 95 | array, and `$.value[*][*]` matches all elements of nested arrays under |
| 96 | `value`. |
| 97 | |
| 98 | `json_extract` reports an error if this parameter isn't a valid |
| 99 | JSONPath string. |
| 100 | |
| 101 | ## Return value |
| 102 | |
| 103 | `json_extract` returns a vector that contains all values matched by the |
| 104 | JSONPath expression. The extracted values keep their JSON types. |
| 105 | |
| 106 | ## See also |
| 107 | |
| 108 | - {doc}`between` |
| 109 | - {ref}`extractor-json` |
| 110 | - {doc}`/reference/commands/select` |