| 1 | <!-- groonga-command --> |
| 2 | <!-- database: extractors_extractor_json --> |
| 3 | |
| 4 | (extractor-json)= |
| 5 | |
| 6 | # `ExtractorJSON` |
| 7 | |
| 8 | ## Summary |
| 9 | |
| 10 | ```{versionadded} 16.0.6 |
| 11 | |
| 12 | ``` |
| 13 | |
| 14 | ```{note} |
| 15 | This is an experimental feature. Currently, this feature is still not stable. |
| 16 | ``` |
| 17 | |
| 18 | `ExtractorJSON` extracts values from JSON data by a JSONPath |
| 19 | expression. You can use this extractor to index only the values you |
| 20 | need from JSON without indexing the whole JSON text. |
| 21 | |
| 22 | The extracted values keep their JSON types. For example, a string in |
| 23 | JSON is extracted as a text and an integer in JSON is extracted as an |
| 24 | integer. So you can use `ExtractorJSON` for both full text search |
| 25 | against strings and range search against numbers. |
| 26 | |
| 27 | ```{note} |
| 28 | `ExtractorJSON` requires the |
| 29 | [jsoncons](https://danielaparker.github.io/jsoncons/) library. It isn't |
| 30 | available if Groonga is built without jsoncons. |
| 31 | ``` |
| 32 | |
| 33 | ## Syntax |
| 34 | |
| 35 | `ExtractorJSON` has a required `path` parameter: |
| 36 | |
| 37 | ``` |
| 38 | ExtractorJSON("path", "JSONPath") |
| 39 | ``` |
| 40 | |
| 41 | ## Usage |
| 42 | |
| 43 | Here is an example that extracts all elements of the `tags` array by |
| 44 | the `$.tags[*]` JSONPath. The `title` value isn't extracted because it |
| 45 | doesn't match the JSONPath: |
| 46 | |
| 47 | <!-- groonga-command --> |
| 48 | |
| 49 | ```{include} ../../example/reference/extractors/extractor_json/string.md |
| 50 | extract \ |
| 51 | --extractors 'ExtractorJSON("path", "$.tags[*]")' \ |
| 52 | --value '{"tags": ["groonga", "search", "engine"], "title": "ignored"}' |
| 53 | ``` |
| 54 | |
| 55 | A JSONPath can match nested values. The following example extracts all |
| 56 | elements of nested arrays by the `$.values[*][*]` JSONPath: |
| 57 | |
| 58 | <!-- groonga-command --> |
| 59 | |
| 60 | ```{include} ../../example/reference/extractors/extractor_json/nested.md |
| 61 | extract \ |
| 62 | --extractors 'ExtractorJSON("path", "$.values[*][*]")' \ |
| 63 | --value '{"values": [[1, 10], [100, 1000]]}' |
| 64 | ``` |
| 65 | |
| 66 | When you attach `ExtractorJSON` to a lexicon, the lexicon indexes the |
| 67 | extracted values. The original JSON is kept in the data column. |
| 68 | |
| 69 | The following example indexes integers in a JSON column. The lexicon |
| 70 | key type is `Int32` because the extracted values are integers. The |
| 71 | index is used automatically when the JSON column is loaded, so you can |
| 72 | search the original records by the extracted values: |
| 73 | |
| 74 | <!-- groonga-command --> |
| 75 | |
| 76 | ```{include} ../../example/reference/extractors/extractor_json/index.md |
| 77 | table_create Data TABLE_NO_KEY |
| 78 | column_create Data value COLUMN_SCALAR JSON |
| 79 | |
| 80 | table_create Numbers TABLE_PAT_KEY Int32 \ |
| 81 | --extractors 'ExtractorJSON("path", "$.value[*][*]")' |
| 82 | column_create Numbers data_value COLUMN_INDEX Data value |
| 83 | |
| 84 | load --table Data |
| 85 | [ |
| 86 | {"value": "{\"value\": [[1, 10], [100]]}"}, |
| 87 | {"value": "{\"value\": [[2], [20, 200]]}"}, |
| 88 | {"value": "{\"value\": [[-1, -10], [-100]]}"} |
| 89 | ] |
| 90 | |
| 91 | select Data --filter 'between(Numbers.data_value, 10, 20)' |
| 92 | ``` |
| 93 | |
| 94 | The records whose extracted values are between `10` and `20` are |
| 95 | selected even though the values are stored in nested arrays in JSON. |
| 96 | |
| 97 | ## Parameters |
| 98 | |
| 99 | ### Required parameter |
| 100 | |
| 101 | (extractor-json-path)= |
| 102 | |
| 103 | #### `path` |
| 104 | |
| 105 | Specifies a JSONPath expression that matches the values to extract. |
| 106 | |
| 107 | A JSONPath expression begins with `$`, which means the root of the |
| 108 | JSON value. For example, `$.tags[*]` matches all elements of the |
| 109 | `tags` array, and `$.values[*][*]` matches all elements of nested |
| 110 | arrays under `values`. |
| 111 | |
| 112 | This parameter is required. `ExtractorJSON` reports an error if this |
| 113 | parameter is missing. |
| 114 | |
| 115 | ## See also |
| 116 | |
| 117 | - {doc}`/reference/extractors` |
| 118 | - {doc}`/reference/commands/table_create` |