Skip to content

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

Closed
@acarbonetto

Description

@acarbonetto

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

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions