Repository navigation
Closed
Labels
Description
Is your feature request related to a problem?
We need to enhance OpenSearch PPL (Piped Processing Language) by adding support for JSON-based operations. This extension will provide users with powerful tools to manipulate and analyze JSON data within PPL queries.
Proposed New Functions
json_object(): Creates a JSON object from existing key/value pairs.
... | eval person = json_object("name", "John", "age", 30)json(): Evaluates whether a value can be parsed as JSON. Returns the value if valid, null otherwise.
... | eval result = json('{"name":"John","age":30}')json_append(): Appends elements to the contents of a valid JSON object.
... | eval person = json_object("name", "John"), extended_person = json_append(person, "city", "New York")json_array(): Creates a JSON array using a list of values.
... | eval json_list = json_array("apple", "banana", "cherry")json_array_to_mv(): Maps the elements of a JSON array to a multivalued field.
... | eval json_list = json_array("apple", "banana", "cherry"), mv_field = json_array_to_mv(json_list)json_delete(): Removes one or more keys and their corresponding values from the specified JSON object.
... | eval person = json_object("name", "John", "age", 30, "city", "New York"), person_no_age = json_delete(person, "age")json_extend(): Extends the contents of a valid JSON object with the values of an array.
... | eval person = json_object("name", "John", "age", 30), extended_person = json_extend(person, json_array("city", "New York"))json_extract(): Returns either a JSON array or a native type value from a field and zero or more paths.
... | eval data = json('{"person": {"name": "John", "age": 30}}'), name = json_extract(data, "person.name")json_keys(): Returns the keys from the key-value pairs in a JSON object as a JSON array.
... | eval person = json_object("name", "John", "age", 30), keys = json_keys(person)json_set(): Inserts or overwrites values for a JSON node with provided values and returns an updated JSON object.
... | eval person = json_object("name", "John"), updated_person = json_set(person, "age", 30)json_valid(): Evaluates whether a JSON object uses valid JSON syntax and returns TRUE or FALSE.
... | eval is_valid = json_valid('{"name":"John","age":30}')all(): Iterates over JSON array values and returns true if every value matches the provided predicate.
... | eval json_list = json_array(1, 2, 3), all_match = all(json_list, val -> val != 0)any(): Iterates over JSON array values and returns true if any value matches the provided predicate.
... | eval json_list = json_array(1, 2, 3), any_match = any(json_list, val -> val == 0)filter(): Iterates over JSON array values and performs an operation on each value.
... | eval json_list = json_array(1, 2, 3, 4), filtered_list = filter(json_list, val -> val % 2 == 0)map(): Iterates over JSON array values and performs an operation on each value.
... | eval json_list = json_array(1, 2, 3), mapped_list = map(json_list, val -> val * 2)reduce(): Iterates over a JSON array in a field or a literal array and performs an accumulation operation.
... | eval json_list = json_array(1, 2, 3), sum = reduce(json_list, 0, (acc, val) -> acc + val)Technical Considerations
- Consider leveraging existing JSON libraries for efficient parsing and manipulation.
- Ensure that these functions can handle large JSON objects without significant performance degradation.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNew
- StatusShow more project fieldsDone
- StatusShow more project fieldsDone
Activity
acarbonetto commentedon Dec 17, 2024
Proposal Part I: Implement JSON validation and JSON objects:
json_valid(string) ->boolean` function. [FEATURE] Addjson_validas a PPL function #3207json_object(key, scalar_value [, key, scalar_value])->json_objectfunction, where scalar_value is of a scalar expression. [FEATURE] Addjson_objectconstructor function to PPL #3208json_object(key, value [, key, value]*)->json_objectfunction, where value is a scalar or json_object. [FEATURE] Addjson_objectconstructor function to PPL #3208json_object(key, value [, key, value]*)->json_objectfunction, where value can be a field expression (for multi-valued fields, take the first value). [FEATURE] Addjson_objectconstructor function to PPL #3208json_object(nested)->json_objectfunction, which casts a nested object into a json object.json(string) -> json_object|nullfunction that validates string json and returns an object or null. [FEATURE] Addjsonfunction to cast string to json object #3209json(nested) -> json_objectfunction that casts a nested object to json. [FEATURE] Addjsonfunction to cast nested to json object #3216to_json_string(json_object) -> stringthat returns the string representation of a json object. [FEATURE] Addto_json_stringfunction to PPL #3210Proposal Part II: Implement JSON read-only functions:
json_extract(json_object (, path)+) -> string- returns a json-encoded string containing the json object or scalar at the designated path. [FEATURE] Addjson_extractfunction to PPL #3211json_keys(json_object) -> array; note: consider adding a path variable to this function. [FEATURE] Adjson_keysfunction to PPL #3212json_delete(json_object[ ,path]+) -> json_objectdeletes the kv-pairs at path; returns the updated json_object. [FEATURE] Addjson_deletefunction to PPL #3213json_set(json_object [, path, value]+) -> json_objectoverwrites the json value at path; and returns the updated json_object. [FEATURE] Addjson_setfunction to PPL #3214Proposal Part III: Add JSON_ARRAY support:
json_array(scalar_value)that returns a json-encoded string with an array of scalars. [FEATURE] Addjson_arrayconstructor function to PPL #3217json_array(field)that returns a json-encoded string with an array of values where field can be a multi-value. [FEATURE] Addjson_arrayconstructor function to PPL #3217json_array(json_object)that returns a json-encoded string with objects. [FEATURE] Addjson_arrayconstructor function to PPL #3217json_array(json_array)that returns a json-encoded string with multi-dimensional arrays. [FEATURE] Addjson_arrayconstructor function to PPL #3217json_arrayas part of the json_object construction;json_object(key, value [, key, value]*)->json_objectfunction, where value can also be a `json_array. [FEATURE] Addjson_objectconstructor function to PPL #3208 [FEATURE] Addjson_arrayconstructor function to PPL #3217json_array_lengthfunction that takes a json doc and returns the number of items in the array at the outmost level; consider adding an optional path variable to this function. [FEATURE] Addjson_array_lengthfunction to PPL #3218json_append(json_object [, path, value]+) -> json_objectadds the value to the end of the array at path; returns the updated json_object. note: value is a single scalar field or value. [FEATURE] Addjson_appendandjson_extendfunctions to PPL #3219json_extend(json_object [, path, value]+) -> json_objectadds the value to the array at path (assuming path is an array type); returns the updated json_object. note: value is an array or multi-value field. json_extend will flatten the values before adding them to the json document array. [FEATURE] Addjson_appendandjson_extendfunctions to PPL #3219Proposal Part IV: Add JSON_ARRAY function support:
Proposed Part I doc:
acarbonetto commentedon Dec 17, 2024
Proposed Part II Documentation
YANG-DB commentedon Dec 18, 2024
Thanks @acarbonetto this looks great!
Can you please:
we can start discussing each function syntax and details within its own issue
Thanks !
[-][FEATURE]Add PPL `JSON` extended functions support[/-][+][RFC] Add PPL `JSON` extended functions support[/+]LantaoJin commentedon Jun 16, 2025
Resolved by #3559 and #3584