Repository navigation
Closed
Labels
Description
Is your feature request related to a problem?
As part of the RFC to add JSON functions, the json_append and json_extend functions allow users to manipulate the json document by adding elements to a json array. json_extend flattens arrays before appending the values to the json document array.
- META RFC: [META]Add PPL
JSONextended functions support #3027 - PPL RFC: [RFC] Add PPL
JSONextended functions support #3028 - Related Spark PPL PR: PPL support json_delete, append functions opensearch-spark#971
What solution would you like?
### `JSON_APPEND`
**Description**
`json_append(doc, [path_key, list of values to add]...)` appends values to end of an array at path within the json document. Return the updated json object.
**Argument type:** JSON_OBJECT, List<STRING>
**Return type:** JSON_OBJECT
**Note**
Append adds the value to the end of the existing array with the following cases:
- path is an object value - append is ignored and the value is returned
- path is an existing array - the value are added to the array's tail
- path not found - the value are added to the root of the json tree
`json_append` differs from `json_extend` in that it does not flatten arrays before appending them.
Example:
os> source=people | eval append = json_append(json('{"teacher":["Alice"],"student":[{"name":"Bob","rank":1},{"name":"Charlie","rank":2}]}'),'student', json('{"name":"Tommy","rank":5}')) | head 1 | fields append
fetched rows / total rows = 1/1
+-----------------------------------------------------------------------------------------------------------------------------------+
| append |
+-----------------------------------------------------------------------------------------------------------------------------------+
|{"teacher":["Alice"],"student":[{"name":"Bob","rank":1},{"name":"Charlie","rank":2},{"name":"Tommy","rank":5}]} |
+-----------------------------------------------------------------------------------------------------------------------------------+
os> source=people | eval append = json_append(json('{"teacher":["Alice"],"student":[{"name":"Bob","rank":1},{"name":"Charlie","rank":2}]}`,'teacher', json_array('Tom', 'Walt')) | head 1 | fields append
fetched rows / total rows = 1/1
+-----------------------------------------------------------------------------------------------------------------------------------+
| append |
+-----------------------------------------------------------------------------------------------------------------------------------+
|{"teacher":["Alice",["Tom","Walt"]],"student":[{"name":"Bob","rank":1},{"name":"Charlie","rank":2}]} |
+-----------------------------------------------------------------------------------------------------------------------------------+
os> source=people | eval append = json_append(`{"school":{"teacher":["Alice"],"student":[{"name":"Bob","rank":1},{"name":"Charlie","rank":2}]}}`, 'school.teacher', 'Tom', 'school.teacher', 'Walt') | head 1 | fields append
fetched rows / total rows = 1/1
+-------------------------------------------------------------------------------------------------------------------------+
| append |
+-------------------------------------------------------------------------------------------------------------------------+
|{"school":{"teacher":["Alice","Tom","Walt"],"student":[{"name":"Bob","rank":1},{"name":"Charlie","rank":2}]}} |
+-------------------------------------------------------------------------------------------------------------------------+
### `JSON_EXTEND`
**Description**
`json_extend(doc, [path_key, list of values to add]...)` extends an array at path within the json document. Return the updated json document.
**Argument type:** JSON_OBJECT, List<STRING>
**Return type:** JSON_OBJECT
**Note**
Append adds the value to the end of the existing array with the following cases:
- path is an object value - append is ignored and the value is returned
- path is an existing array - the value are added to the array's tail
- path not found - the value are added to the root of the json tree
`json_extend` differs from `json_append` in that it flattens arrays before appending them the json document arrays.
Example:
os> source=people | eval extend = json_extend(json('{"teacher":["Alice"],"student":[{"name":"Bob","rank":1},{"name":"Charlie","rank":2}]}'),'student', json('{"name":"Tommy","rank":5}')) | head 1 | fields append
fetched rows / total rows = 1/1
+-----------------------------------------------------------------------------------------------------------------------------------+
| extend |
+-----------------------------------------------------------------------------------------------------------------------------------+
|{"teacher":["Alice"],"student":[{"name":"Bob","rank":1},{"name":"Charlie","rank":2},{"name":"Tommy","rank":5}]} |
+-----------------------------------------------------------------------------------------------------------------------------------+
os> source=people | eval extend = json_extend(json('{"teacher":["Alice"],"student":[{"name":"Bob","rank":1},{"name":"Charlie","rank":2}]}`,'teacher', json_array('Tom', 'Walt')) | head 1 | fields append
fetched rows / total rows = 1/1
+-----------------------------------------------------------------------------------------------------------------------------------+
| extend |
+-----------------------------------------------------------------------------------------------------------------------------------+
|{"teacher":["Alice","Tom","Walt"],"student":[{"name":"Bob","rank":1},{"name":"Charlie","rank":2}]} |
+-----------------------------------------------------------------------------------------------------------------------------------+
os> source=people | eval extend = json_extend(`{"school":{"teacher":["Alice"],"student":[{"name":"Bob","rank":1},{"name":"Charlie","rank":2}]}}`, 'school.teacher', 'Tom', 'school.teacher', 'Walt') | head 1 | fields append
fetched rows / total rows = 1/1
+-------------------------------------------------------------------------------------------------------------------------+
| extend |
+-------------------------------------------------------------------------------------------------------------------------+
|{"school":{"teacher":["Alice","Tom","Walt"],"student":[{"name":"Bob","rank":1},{"name":"Charlie","rank":2}]}} |
+-------------------------------------------------------------------------------------------------------------------------+
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
Type
Projects
- StatusShow more project fieldsDone
Activity
LantaoJin commentedon Jun 16, 2025
Resolved by #3559