Skip to content

Commit 9be182a

Browse files
andrew-colemanmattbaileyuk
authored andcommitted
release v1.8.0
Signed-off-by: andrew-coleman <andrew_coleman@uk.ibm.com>
1 parent f3dadcd commit 9be182a

File tree

6 files changed

+253
-1
lines changed

6 files changed

+253
-1
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
#### 1.8.0 Milestone Release
2+
3+
- New syntax (`%` parent operator) to select the parent of the current context value (issue #299)
4+
- New function `$type` to return the data type of the argument (issue #208)
5+
- Added versioning to the documentation site (issue #385)
6+
- Fixed bugs #382, #387, #396, #399
7+
18
#### 1.7.0 Milestone Release
29

310
- New syntax (`@` operator) to support cross-referencing and complex data joins (issue #333)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsonata",
3-
"version": "1.7.0",
3+
"version": "1.8.0",
44
"description": "JSON query and transformation language",
55
"module": "jsonata.js",
66
"main": "jsonata.js",

website/i18n/en.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,14 @@
226226
"version-1.7.0/version-1.7.0-using-nodejs": {
227227
"title": "Using JSONata in a Node application",
228228
"sidebar_label": "In NodeJS"
229+
},
230+
"version-1.8.0/version-1.8.0-object-functions": {
231+
"title": "Object functions",
232+
"sidebar_label": "Object Functions"
233+
},
234+
"version-1.8.0/version-1.8.0-path-operators": {
235+
"title": "Path Operators",
236+
"sidebar_label": "Path Operators"
229237
}
230238
},
231239
"links": {
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
id: version-1.8.0-object-functions
3+
title: Object functions
4+
sidebar_label: Object Functions
5+
original_id: object-functions
6+
---
7+
8+
## `$keys()`
9+
__Signature:__ `$keys(object)`
10+
11+
Returns an array containing the keys in the object. If the argument is an array of objects, then the array returned contains a de-duplicated list of all the keys in all of the objects.
12+
13+
## `$lookup()`
14+
__Signature:__ `$lookup(object, key)`
15+
16+
Returns the value associated with `key` in `object`. If the first argument is an array of objects, then all of the objects in the array are searched, and the values associated with all occurrences of `key` are returned.
17+
18+
19+
## `$spread()`
20+
__Signature:__ `$spread(object)`
21+
22+
Splits an `object` containing key/value pairs into an array of objects, each of which has a single key/value pair from the input `object`. If the parameter is an array of objects, then the resultant array contains an object for every key/value pair in every object in the supplied array.
23+
24+
## `$merge()`
25+
__Signature:__ `$merge(array<object>)`
26+
27+
Merges an array of objects into a single object containing all the key/value pairs from each of the objects in the input array. If any of the input objects contain the same key, then the returned object will contain the value of the last one in the array. It is an error if the input array contains an item that is not an object.
28+
29+
## `$sift()`
30+
__Signature:__ `$sift(object, function)`
31+
32+
See definition under 'Higher-order functions'
33+
34+
## `$each()`
35+
__Signature:__ `$each(object, function)`
36+
37+
Returns an array containing the values return by the `function` when applied to each key/value pair in the `object`.
38+
39+
The `function` parameter will get invoked with two arguments:
40+
41+
`function(value, name)`
42+
43+
where the `value` parameter is the value of each name/value pair in the object and `name` is its name. The `name` parameter is optional.
44+
45+
__Examples__
46+
47+
`$each(Address, function($v, $k) {$k & ": " & $v})`
48+
49+
=>
50+
51+
[
52+
"Street: Hursley Park",
53+
"City: Winchester",
54+
"Postcode: SO21 2JN"
55+
]
56+
57+
## `$error()`
58+
__Signature:__`$error(message)`
59+
60+
Deliberately throws an error with an optional `message`
61+
62+
## `$assert()`
63+
__Signature:__`$assert(condition, message)`
64+
65+
If condition is true, the function returns undefined. If the condition is false, an exception is thrown with the message as the message of the exception.
66+
67+
## `$type()`
68+
__Signature:__`$type(value)`
69+
70+
Evaluates the type of `value` and returns one of the following strings:
71+
* `"null"`
72+
* `"number"`
73+
* `"string"`
74+
* `"boolean"`
75+
* `"array"`
76+
* `"object"`
77+
* `"function"`
78+
Returns (non-string) `undefined` when `value` is `undefined`.
79+
80+
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
---
2+
id: version-1.8.0-path-operators
3+
title: Path Operators
4+
sidebar_label: Path Operators
5+
original_id: path-operators
6+
---
7+
8+
The path operators underpin the declarative nature of the map/filter/reduce processing model in JSONata.
9+
10+
## `.` (Map)
11+
12+
The dot operator is one of the fundamental building blocks in JSONata expressions. It implements the 'for each' or 'map' function that is common in many functional languages.
13+
14+
The dot operator performs the following logic:
15+
16+
- The expression on the LHS is evaluated to produce an array of values.
17+
- If it evaluates to a single value, that is treated as equivalent to an array containing that single value
18+
- If it evaluates to nothing (no match or empty array), then the result of the operator expression is nothing
19+
- For each value in the LHS array in turn:
20+
- The value is known as the _context_ and is used as the basis for any relative path expression on the RHS. It is also accessible in the RHS expression using the `$` symbol.
21+
- The RHS expression is evaluated to produce a value or array of values (or nothing). These values are appended to a combined array of results for the operator as a whole.
22+
- The combined result of the operator is returned.
23+
24+
This operator is left associative meaning that the expression `a.b.c.d` is evaluated like `((a.b).c).d`; i.e. left to right
25+
26+
__Examples__
27+
28+
- `Address.City` => `"Winchester"`
29+
- `Phone.number` => `[ "0203 544 1234", "01962 001234", "01962 001235", "077 7700 1234" ]`
30+
- `Account.Order.Product.(Price * Quantity)` => `[ 68.9, 21.67, 137.8, 107.99 ]`
31+
- `Account.Order.OrderID.$uppercase()` => `[ "ORDER103", "ORDER104"]`
32+
33+
## `[` ... `]` (Filter)
34+
35+
The filter operator (a.k.a predicate) is used to select only the items in the input sequence that satisfy the predicate expression contained between the square brackets.
36+
37+
If the predicate expression is an integer, or an expression that evaluates to an integer, then the item at that position (zero offset) in the input sequence is the only item selected for the result sequence.
38+
If the number is non-integer, then it is rounded _down_ to the nearest integer.
39+
40+
If the predicate expression is an array of integers, or an expression that evaluates to an array of integers, then the items at those positions (zero offset) in the input sequence is the only item selected for the result sequence.
41+
42+
If the predicate expression evaluates to any other value, then it is cast to a Boolean as if using the `$boolean()` function. If this evaluates to `true`, then the item is retained in the result sequence. Otherwise it is rejected.
43+
44+
See [Navigating JSON Arrays](simple#navigating-json-arrays) and [Predicates](predicate) for more details and examples.
45+
46+
## `^(` ... `)` (Order-by)
47+
48+
The order-by operator is used to sort an array of values into ascending or descending order according to one or more expressions defined within the parentheses.
49+
50+
By default, the array will be sorted into ascending order. For example:
51+
52+
`Account.Order.Product^(Price)`
53+
54+
sorts all of the products into order of increasing price (`Price` is a numeric field in the `Product` object).
55+
56+
To sort in descending order, the sort expression must be preceded by the `>` symbol. For example:
57+
58+
`Account.Order.Product^(>Price)`
59+
60+
sorts all of the products into order of decreasing price. The `<` symbol can be used explicitly indicate ascending order, although that is the default behaviour.
61+
62+
Secondary (and more) sort expressions can be specified by separating them with commas (`,`). The secondary expression will be used to determine order if the primary expression ranks two values the same. For example,
63+
64+
`Account.Order.Product^(>Price, <Quantity)`
65+
66+
orders the products primarily by decreasing price, but for products of the same price, by increasing quantity.
67+
68+
The sort expression(s) can be any valid JSONata expression that evaluates to a number or a string. If it evaluates to a string then the array is sorted in order of unicode codepoint.
69+
70+
__Examples__
71+
72+
- `Account.Order.Product^(Price * Quantity)` => Increasing order of price times quantity.
73+
- `student[type='fulltime']^(DoB).name` => The names of all full time students sorted by date of birth (the DoB value is an ISO 8601 date format)
74+
75+
## `{` ... `}` (Reduce)
76+
77+
The reduce operator can be used as the last step in a path expression to group and aggregate its input sequence into a single object.
78+
The key/value pairs between the curly braces determine the groupings (by evaluating the key expression) and the aggregated values for each group.
79+
See [Grouping and Aggregation](sorting-grouping#grouping) for more details.
80+
81+
82+
## `*` (Wildcard)
83+
84+
This wildcard selects the values of all the properties of the context object. It can be used in a path expression in place of a property name, but it cannot be combined with other characters like a glob pattern. The order of these values in the result sequence is implementation dependent.
85+
See [Wildcards](predicate#wildcards) for examples.
86+
87+
## `**` (Descendants)
88+
89+
This wildcard recursively selects the values of all the properties of the context object, and the properties of any objects contained within these values as it descends the hierarchy.
90+
See [Navigate arbitrary depths](predicate#navigate-arbitrary-depths).
91+
92+
## `%` (Parent)
93+
94+
This will select the 'parent' of the current context value. Here, we define 'parent' to be the enclosing object which has the property representing the context value.
95+
96+
This is the only operation which searches 'backwards' in the input data structure. It is implemented by static analysis of the expression at [compile time](https://docs.jsonata.org/embedding-extending#jsonatastr) and can only be used within expressions that navigate through that target parent value in the first place.
97+
If, for any reason, the parent location cannot be determined, then a static error (S0217) is thrown.
98+
99+
__Example__
100+
101+
```
102+
Account.Order.Product.{
103+
'Product': `Product Name`,
104+
'Order': %.OrderID,
105+
'Account': %.%.`Account Name`
106+
}
107+
```
108+
This returns an array of objects for each product in each order in each account. Information from the enclosing Order and Account objects can be accessed using the parent operator.
109+
The repeated combination of `%.%.` is used to access the grandparent and higher ancestors.
110+
111+
112+
## `#` (Positional variable binding)
113+
114+
This can be used to determine at which position in the sequence the current context item is. It can be used following any map, filter or order-by stage in the path.
115+
The variable is available for use within subsequent stages of the path (e.g. within filter predicates) and goes out of scope at the end of the path expression.
116+
117+
__Example__
118+
119+
```
120+
library.books#$i['Kernighan' in authors].{
121+
'title': title,
122+
'index': $i
123+
}
124+
```
125+
This returns an array of objects for each book in the library where Kernighan is one of the authors. Each object contains the book's title and its position within the books array before it was filtered.
126+
127+
128+
## `@` (Context variable binding)
129+
130+
This is used to bind the current context item (`$`) to a named variable. It can only be used directly following a map stage, not a filter or order-by stage.
131+
The variable binding remains in scope for the remainder of the path expression.
132+
133+
Because the current context has now been explicitly bound to a named variable, this context will be carried forward to be the context of the next stage in the path.
134+
For example, in this snippet of a path, `library.loans@$l.books`, the loans array is a property of the library object and each loan will, in turn, be bound to the variable `$l`.
135+
The books array, which is also a property of the library object, will then be selected.
136+
137+
This operator can be used to perform data joins within a path because of its ability to do cross-referencing across objects.
138+
139+
__Example__
140+
141+
```
142+
library.loans@$l.books@$b[$l.isbn=$b.isbn].{
143+
'title': $b.title,
144+
'customer': $l.customer
145+
}
146+
```
147+
This performs an 'inner join' between objects in the loans array and objects in the books array where the ISBNs match between the structures.
148+
149+
Block expressions can be used to widen the scope of the data cross-referencing as shown in this example:
150+
151+
```
152+
(library.loans)@$l.(catalog.books)@$b[$l.isbn=$b.isbn].{
153+
'title': $b.title,
154+
'customer': $l.customer
155+
}
156+
```

website/versions.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
[
2+
"1.8.0",
23
"1.7.0"
34
]

0 commit comments

Comments
 (0)