Repository navigation
Closed
Labels
Description
Is your feature request related to a problem?
As part of the RFC to add JSON functions, the json_extract function would be useful to extract specific objects and scalars from an existing json_object.
- META RFC: [META]Add PPL
JSONextended functions support #3027 - PPL RFC: [RFC] Add PPL
JSONextended functions support #3028 - Related Spark PPL PR: Support PPL
JSONfunctions: construction and extraction opensearch-spark#780
What solution would you like?
### `JSON_EXTRACT`
**Description**
`json_extract(doc, path [, path]...)` Extracts a json value or scalar from a json document based on the path(s) specified.
**Argument type:** STRING, STRING
**Return type:** STRING | NULL | JSON ARRAY
- Returns a JSON array for multiple paths or if the path leads to an array.
- Return null if path is not valid.
Example:
os> source=people | eval `json_extract(json('{"a":"b"}'), '$.a')` = json_extract('{"a":"b"}', '$a')
fetched rows / total rows = 1/1
+----------------------------------+
| json_extract('{"a":"b"}', 'a') |
+----------------------------------+
| b |
+----------------------------------+
os> source=people | eval `json_extract(json('{"a":[{"b":1},{"b":2}]}'), '$.a[1].b')` = json_extract('{"a":[{"b":1},{"b":2}]}', '$.a[1].b')
fetched rows / total rows = 1/1
+-----------------------------------------------------------+
| json_extract('{"a":[{"b":1.0},{"b":2.0}]}', '$.a[1].b') |
+-----------------------------------------------------------+
| 2.0 |
+-----------------------------------------------------------+
os> source=people | eval `json_extract('{"a":[{"b":1},{"b":2}]}', '$.a[*].b')` = json_extract(json('{"a":[{"b":1},{"b":2}]}'), '$.a[*].b')
fetched rows / total rows = 1/1
+-----------------------------------------------------------+
| json_extract('{"a":[{"b":1.0},{"b":2.0}]}', '$.a[*].b') |
+-----------------------------------------------------------+
| [1.0,2.0] |
+-----------------------------------------------------------+
os> source=people | eval `invalid_json` = json_extract(json('{"invalid": "json"'))
fetched rows / total rows = 1/1
+----------------+
| invalid_json |
+----------------+
| null |
+----------------+
What alternatives have you considered?
N/A
Do you have any additional context?
Related spark PPL PR: opensearch-project/opensearch-spark#780
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
kenrickyap commentedon Jan 17, 2025
Hi @YANG-DB @LantaoJin currently working on this ticket and was wondering if I could import the following library: https://github.com/json-path/JsonPath. It has APACHE 2.0 licence and would be ideal in handling the path traversal that needs to be done.
This would be more robust than implementing the functionality ourselves and would be useful to other json functions.
YANG-DB commentedon Jan 17, 2025
@kenrickyap I agree
LantaoJin commentedon Jun 16, 2025
Resolved by #3559 with Calcite implementation.