Skip to content

Commit c2d4e49

Browse files
authored
Merge pull request #101 from jmespath-community/jep/object-manipulation-functions
JEP-013 Object Manipulation Functions
2 parents 6351eb2 + cb43c0d commit c2d4e49

File tree

5 files changed

+207
-16
lines changed

5 files changed

+207
-16
lines changed

function_schema.yml

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,37 @@ $schema: https://json-schema.org/draft/2020-12/schema
33
$id: https://jmespath.site/function_schema.yml
44
title: JMESPath function
55
description: JMESPath function definition and tests
6+
$defs:
7+
unnamed_arg: &unnamed_arg
8+
type: object
9+
properties:
10+
type: &type
11+
description: ""
12+
oneOf:
13+
- enum: &types
14+
- any
15+
- number
16+
- string
17+
- boolean
18+
- array
19+
- object
20+
- "null"
21+
- expression
22+
- "array[number]"
23+
- "array[string]"
24+
- "array[boolean]"
25+
- "array[object]"
26+
- "array[any]"
27+
- "array[array[any]]"
28+
- expression->any
29+
- expression->number
30+
- expression->string
31+
- type: array
32+
items:
33+
enum: *types
34+
desc:
35+
type: string
36+
description: ""
637
type: object
738
required: [name, topic, args, returns, desc, examples]
839
properties:
@@ -20,24 +51,19 @@ properties:
2051
type: array
2152
description: ""
2253
items:
23-
type: object
54+
allOf:
55+
- $ref: "#/$defs/unnamed_arg"
56+
- type: object
57+
required:
58+
- name
59+
properties:
60+
name:
61+
type: string
62+
description: ""
2463
description: ""
25-
additionalProperties: false
26-
properties:
27-
name:
28-
type: string
29-
description: ""
30-
type: &type
31-
description: ""
32-
oneOf:
33-
- enum: &types [any, number, string, boolean, array, object, "null", expression, "array[number]", "array[string]", "array[boolean]", "array[object]", "array[any]", "expression->any", "expression->number", "expression->string"]
34-
- type: array
35-
items:
36-
enum: *types
37-
desc:
38-
type: string
39-
description: ""
64+
unevaluatedProperties: false
4065
optional: *arg
66+
variadic: *unnamed_arg
4167
returns:
4268
type: object
4369
description: ""

functions/from_items.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: from_items
2+
topic: misc
3+
args:
4+
required:
5+
- name: arg
6+
type: ['array[any]']
7+
desc: ''
8+
optional: []
9+
returns:
10+
type: [object]
11+
desc: ''
12+
desc: |2
13+
Returns an object from the provided array of key value pairs.
14+
This function is the inversed of the `items()` function.
15+
examples:
16+
from_items:
17+
context: [["one", 1], ["two", 2]]
18+
args: ['@']
19+
returns: {"one": 1, "two": 2}
20+
from_items_duplicate_entry:
21+
context: [["one", 1], ["two", 2], ["one", 3]]
22+
args: ['@']
23+
returns: {"one": 3, "two": 2}

functions/items.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: items
2+
topic: misc
3+
args:
4+
required:
5+
- name: obj
6+
type: [object]
7+
desc: ''
8+
optional: []
9+
returns:
10+
type: ['array[any]']
11+
desc: ''
12+
desc: |2
13+
Returns an array of key-value pairs for the provided input object.
14+
Each pair is a 2-item array with the first item being the key and
15+
the second item being the value.
16+
This function is the inversed of the `from_items()` function.
17+
18+
Note that because JSON hashes are inheritently unordered,
19+
the key value pairs of the provided object $obj are inheritently unordered.
20+
Implementations are not required to return values in any specific order.
21+
examples:
22+
basic:
23+
context: {"a": "first", "b": "second"}
24+
args: ['@']
25+
returns: [["a", "first"], ["b", "second"]]

functions/zip.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: zip
2+
topic: misc
3+
args:
4+
required:
5+
- name: arg
6+
type: ['array[any]']
7+
desc: ''
8+
variadic:
9+
type: ['array[any]']
10+
desc: ''
11+
optional: []
12+
returns:
13+
type: ['array[array[any]]']
14+
desc: ''
15+
desc: |2
16+
Accepts one or more arrays as arguments and returns an array of arrays
17+
in which the _i-th_ array contains the _i-th_ element from each of the
18+
argument arrays.
19+
The returned array is truncated to the length of the shortest argument array.
20+
examples:
21+
zip:
22+
context:
23+
args:
24+
- ["a", "b"]
25+
- [1, 2]
26+
returns: [["a", 1], ["b", 2]]
27+
zip_sparse:
28+
context:
29+
args:
30+
- ["a", "b", "c"]
31+
- [1, 2]
32+
returns: [["a", 1], ["b", 2]]
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Object Manipulation Functions
2+
3+
|||
4+
|---|---
5+
| **JEP** | 13
6+
| **Author** | Jonathan Stewmon
7+
| **Created**| 21-March-2016
8+
| **SemVer** | MINOR
9+
| **Status**| Draft
10+
11+
## Abstract
12+
13+
As a JSON querying language, JMESPath has long needed some functions to manipulate JSON objects.
14+
15+
This JEP introduces built-in functions to extract a list of key/value pairs from a JSON object and conversely converting an array of key/value pairs to a JSON object.
16+
17+
## Specification
18+
19+
### items
20+
21+
```
22+
array[array[any]] items(object $obj)
23+
```
24+
25+
Returns a an array of key value pairs for the provided object `$obj`. Each pair is a 2-item array with the first item being the key and the second item being the value. This function is the inverse of the `from_items()` function.
26+
27+
Note that because JSON hashes are inheritently unordered, the key value pairs of the provided object `$obj` are inheritently unordered. Implementations are not required to return values in any specific order.
28+
29+
For example, given the input:
30+
31+
```json
32+
{"a": "first", "b": "second", "c": "third"}
33+
```
34+
35+
The expression ``items(@)`` could have any of these return values:
36+
37+
- ``[["a", "first"], ["b", "second"], ["c", "third"]]``
38+
- ``[["a", "first"], ["c", "third"], ["b", "second"]]``
39+
- ``[["b", "second"], ["a", "first"], ["c", "third"]]``
40+
- ``[["b", "second"], ["c", "third"], ["a", "first"]]``
41+
- ``[["c", "third"], ["a", "first"], ["b", "second"]]``
42+
- ``[["c", "third"], ["b", "second"], ["a", "first"]]``
43+
44+
If you would like a specific order, consider using the ``sort_by`` function.
45+
46+
### Examples
47+
48+
|Given|Expression|Result
49+
|---|---|---
50+
|``{"a": "first", "b": "second"}``|``items(@)``|``[["b", "second"], ["a", "first"]]``
51+
|``{"z": "last", "b": "second"}``|``sort_by(items(@), &[0])``|``[["b", "second"], ["z", "last"]]``
52+
|``{"z": "last", "b": "second"}``|``sort_by(items(@), &[1])``|``[["z", "last"], ["b", "second"]]``
53+
54+
### from_items
55+
56+
```
57+
object from_items(array[array[any]] $arg)
58+
```
59+
Returns an object from the provided array of key value pairs. This function is the inverse of the `items()`.
60+
61+
### Examples
62+
63+
|Given|Expression|Result
64+
|---|---|---
65+
|``[["one", 1], ["two", 2]]``|``from_items(@)``|``{"one": 1, "two": 2}``
66+
|``[["one", 1], ["two", 2], ["one", 3]]``|``from_items(@)``|``{"one": 3, "two": 2}``
67+
68+
### zip
69+
70+
```
71+
array[array[any]] zip([array[any] $arg, [, array[any] $...]])
72+
```
73+
74+
Accepts one or more arrays as arguments and returns an array of arrays in which the *i-th* array contains the *i-th* element from each of the argument arrays. The returned array is truncated to the length of the shortest argument array.
75+
76+
### Examples
77+
78+
|Expression|Result
79+
|---|---
80+
|``zip(`["a", "b"]`, `[1, 2]`)``|``[["a", 1], ["b", 2]]``
81+
|``zip(`["a", "b", "c"]`, `[1, 2]`)``|``[["a", 1], ["b", 2]]``
82+
83+
## Compliance tests
84+
85+
A new `objects.json` file will be added to the compliance test suite.

0 commit comments

Comments
 (0)