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] Ad json_keys function to PPL #3212

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

[FEATURE] Ad json_keys function to PPL #3212

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_keys function would be useful to extract a list of keys at a path from a json documents

What solution would you like?

### `JSON_KEYS`

**Description**

`json_keys(doc [, path])` Returns an array of all keys of the object at path in the JSON document.  If path is not specified, returns the keys at the root of the JSON document. 

**Argument type:**  OBJECT, STRING

A STRING expression of a valid JSON document.

**Return type:** ARRAY

`json_keys(doc, paths)` Returns an array of all keys of the object at the given paths in the JSON document.  

**Argument type:**  OBJECT, ARRAY

A STRING expression of a valid JSON document.

**Return type:** ARRAY

`NULL` is returned from an invalid JSON.

Example:

    os> source=people | eval `keys` = json_keys(json('{"f1":"abc","f2":{"f3":"a","f4":"b"}}'))
    fetched rows / total rows = 1/1
    +------------+
    | keys       |
    +------------+
    | [f1, f2]   |
    +------------+

    os> source=people | eval `keys` = json_keys(json('{"f1":"abc","f2":{"f3":"a","f4":"b"}}'), "f1", "f2")
    fetched rows / total rows = 1/1
    +------------+
    | keys       |
    +------------+
    | [f3, f4]   |
    +------------+

    os> source=people | eval `keys` = json_keys(json('{"f1":"abc","f2":{"f3":"a","f4":"b"}}'), json_array("{}", "f2"))
    fetched rows / total rows = 1/1
    +------------+
    | keys       |
    +------------+
    | [f1, f2, f3, f4]   |
    +------------+

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

What alternatives have you considered?

N/A

Do you have any additional context?

opensearch-project/opensearch-spark#780 - PR to add json objects 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