Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add json function to cast string to json object #3209

Open
4 tasks
acarbonetto opened this issue Dec 18, 2024 · 0 comments
Open
4 tasks

[FEATURE] Add json function to cast string to json object #3209

acarbonetto opened this issue Dec 18, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@acarbonetto
Copy link
Collaborator

acarbonetto commented Dec 18, 2024

Is your feature request related to a problem?

As part of the RFC to add JSON functions, the json() function would be useful to construct JSON_OBJECTS from json-encoded strings, or cast strings to JSON_OBJECTS.

What solution would you like?

  • Add the json(str) function that constructs a JSON_OBJECT from a string
  • Add the cast(str to json) casting syntax to cast a string to a JSON_OBJECT
  • Add implicit cast from string to json
  • Update documentation and mapping
### `JSON`

**Description**

`json(value)` Evaluates whether a string can be parsed and casted to a JSON object. Returns the JSON object if valid, null otherwise.

**Argument type:** STRING

**Return type:** JSON OBJECT/NULL

Example:

    os> source=people | eval `valid_json()` = json('[1,2,3,{"f1":1,"f2":[5,6]},4]') | fields valid_json
    fetched rows / total rows = 1/1
    +---------------------------------+
    | valid_json                      |
    +---------------------------------+
    | [1,2,3,{"f1":1,"f2":[5,6]},4]   |
    +---------------------------------+

    os> source=people | eval `invalid_json()` = json('{"invalid": "json"') | fields invalid_json
    fetched rows / total rows = 1/1
    +----------------+
    | invalid_json   |
    +----------------+
    | null           |
    +----------------+

JSON Cast:

Example:

    os> source=people | eval valid_json = cast('[1,2,3,{"f1":1,"f2":[5,6]},4]' to json) | fields valid_json
    fetched rows / total rows = 1/1
    +---------------------------------+
    | valid_json                      |
    +---------------------------------+
    | [1,2,3,{"f1":1,"f2":[5,6]},4]   |
    +---------------------------------+

    os> source=people | eval invalid_json = cast('{"invalid": "json"' to json) | fields invalid_json
    fetched rows / total rows = 1/1
    +----------------+
    | invalid_json   |
    +----------------+
    | null           |
    +----------------+

What alternatives have you considered?

N/A

Do you have any additional context?

opensearch-project/opensearch-spark#780 - PR to add feature to opensearch-spark PPL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants