Skip to content

[FEATURE] Add json_extract function to PPL #3211

Closed

Description

@acarbonetto

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.

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

Activity

  1. kenrickyap commented on Jan 17, 2025

    @kenrickyap
    Contributor

    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.

  2. YANG-DB commented on Jan 17, 2025

    @YANG-DB
    Member

    @kenrickyap I agree

  3. added
    PPL
    on May 29, 2025
  4. LantaoJin commented on Jun 16, 2025

    @LantaoJin
    Member

    Resolved by #3559 with Calcite implementation.

  5. moved this from Not Started to Done in PPL Trackeron Jun 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    PPLenhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions