Skip to content

Commit 1c3b1f4

Browse files
andrew-colemanmattbaileyuk
authored andcommitted
Release v2.1.0
Including new version of the documentation site. Signed-off-by: Andrew Coleman <[email protected]>
1 parent 8375728 commit 1c3b1f4

16 files changed

+1818
-1
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
#### 2.1.0 Milestone Release
2+
3+
- New syntax (`?:` default operator) supports fallback to RHS if the LHS is Boolean equivalent to false (PR #784)
4+
- New syntax (`??` coalescing operator) supports fallback to RHS if the LHS is non-existent (PR #784)
5+
- Improve regex generation for DateTime parser (PR #728)
6+
- Truncate fractional part of numeric argument of `$pad` function (PR #729)
7+
- Await array elements (PR #747)
8+
- Various documentation fixes and improvements
9+
110
#### 2.0.6 Maintenance Release
211

312
- Protect __evaluate_entry and __evaluate_exit callbacks (PR #700)

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": "2.0.6",
3+
"version": "2.1.0",
44
"description": "JSON query and transformation language",
55
"module": "jsonata.js",
66
"main": "jsonata.js",
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
---
2+
id: version-2.1.0-construction
3+
title: Building result structures
4+
sidebar_label: Result Structures
5+
original_id: construction
6+
---
7+
8+
So far, we have discovered how to extract values from a JSON document, and how to manipulate the data using numeric, string and other operators. It is useful to be able to specify how this processed data is presented in the output.
9+
10+
## Array constructors
11+
12+
As previously observed, when a location path matches multiple values in the input document, these values are returned as an array. The values might be objects or arrays, and as such will have their own structure, but the _matched values_ themselves are at the top level in the resultant array.
13+
14+
It is possible to build extra structure into the resultant array by specifying the construction of arrays (or [objects](#object-constructors)) within the location path expression. At any point in a location path where a field reference is expected, a pair of square brackets `[]` can be inserted to specify that the results of the expression within those brackets should be contained within a new array in the output. Commas are used to separate multiple expressions within the array constructor.
15+
16+
Array constructors can also be used within location paths for making multiple selections without the broad brush use of wildcards.
17+
18+
__Examples__
19+
20+
- The four email addresses are returned in a flat array.
21+
<div class="jsonata-ex">
22+
<div>Email.address</div>
23+
<div>[
24+
25+
26+
27+
28+
]</div>
29+
</div>
30+
31+
- Each email object generates an array of addresses.
32+
<div class="jsonata-ex">
33+
<div>Email.[address]</div>
34+
<div>[
35+
36+
37+
]</div>
38+
</div>
39+
40+
- Selects the `City` value of both `Address` and `Alternative.Address` objects.
41+
<div class="jsonata-ex">
42+
<div>[Address, Other.`Alternative.Address`].City</div>
43+
<div>[ "Winchester", "London" ]</div>
44+
</div>
45+
46+
## Object constructors
47+
48+
In a similar manner to the way arrays can be constructed, JSON objects can also be constructed in the output. At any point in a location path where a field reference is expected, a pair of braces `{}` containing key/value pairs separated by commas, with each key and value separated by a colon: `{key1: value1, key2:value2}`. The keys and values can either be literals or can be expressions. The key must either be a string or an expression that evaluates to a string.
49+
50+
When an object constructor follows an expression that selects multiple values, the object constructor will create a single object that contains a key/value pair for each of those context values. If an array of objects is required (one for each context value), then the object constructor should immediately follow the dot '.' operator.
51+
52+
__Examples__
53+
54+
- Produces an array of objects (one for each phone).
55+
<div class="jsonata-ex">
56+
<div>Phone.{type: number}</div>
57+
<div>[
58+
{ "home": "0203 544 1234" },
59+
{ "office": "01962 001234" },
60+
{ "office": "01962 001235" },
61+
{ "mobile": "077 7700 1234" }
62+
]</div>
63+
</div>
64+
65+
- Combines the key/value pairs into a single object. See [Grouping using object key expression](sorting-grouping.md) for more details.
66+
<div class="jsonata-ex">
67+
<div>Phone{type: number}</div>
68+
<div>{
69+
"home": "0203 544 1234",
70+
"office": [
71+
"01962 001234",
72+
"01962 001235"
73+
],
74+
"mobile": "077 7700 1234"
75+
}</div>
76+
</div>
77+
78+
- Combines the key/value pairs into a single object. In this case, for consistency, all numbers are grouped into arrays. See [Singleton array and value equivalence](predicate.md#singleton-array-and-value-equivalence) for more details.
79+
<div class="jsonata-ex">
80+
<div>Phone{type: number[]}</div>
81+
<div>{
82+
"home": [
83+
"0203 544 1234"
84+
],
85+
"office": [
86+
"01962 001234",
87+
"01962 001235"
88+
],
89+
"mobile": [
90+
"077 7700 1234"
91+
]
92+
}</div>
93+
</div>
94+
95+
96+
## JSON literals
97+
98+
The array and object constructors use the standard JSON syntax for JSON arrays and JSON objects. In addition to this values of the other JSON data types can be entered into an expression using their native JSON syntax:
99+
- strings - `"hello world"`
100+
- numbers - `34.5`
101+
- Booleans - `true` or `false`
102+
- nulls - `null`
103+
- objects - `{"key1": "value1", "key2": "value2"}`
104+
- arrays - `["value1", "value2"]`
105+
106+
__JSONata is a superset of JSON.__ This means that any valid JSON document is also a valid JSONata expression. This property allows you to use a JSON document as a template for the desired output, and then replace parts of it with expressions to insert data into the output from the input document.
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
---
2+
id: version-2.1.0-embedding-extending
3+
title: Embedding and Extending JSONata
4+
sidebar_label: Embedding and Extending JSONata
5+
original_id: embedding-extending
6+
---
7+
8+
## API
9+
10+
### jsonata(str)
11+
12+
Parse a string `str` as a JSONata expression and return a compiled JSONata expression object.
13+
14+
```javascript
15+
var expression = jsonata("$sum(example.value)");
16+
```
17+
18+
If the expression is not valid JSONata, an `Error` is thrown containing information about the nature of the syntax error, for example:
19+
20+
```
21+
{
22+
code: "S0202",
23+
stack: "...",
24+
position: 16,
25+
token: "}",
26+
value: "]",
27+
message: "Syntax error: expected ']' got '}'"
28+
}
29+
```
30+
31+
`expression` has three methods:
32+
33+
### expression.evaluate(input[, bindings[, callback]])
34+
35+
Run the compiled JSONata expression against object `input` and return the result as a new object.
36+
37+
```javascript
38+
var result = await expression.evaluate({example: [{value: 4}, {value: 7}, {value: 13}]});
39+
```
40+
41+
`input` should be a JavaScript value such as would be returned from `JSON.parse()`. If `input` could not have been parsed from a JSON string (is circular, contains functions, ...), `evaluate`'s behaviour is not defined. `result` is a new JavaScript value suitable for `JSON.stringify()`ing.
42+
43+
`bindings`, if present, contain variable names and values (including functions) to be bound:
44+
45+
```javascript
46+
await jsonata("$a + $b()").evaluate({}, {a: 4, b: () => 78});
47+
// returns 82
48+
```
49+
50+
`expression.evaluate()` may throw a run-time `Error`:
51+
52+
```javascript
53+
var expression = jsonata("$notafunction()"); // OK, valid JSONata
54+
await expression.evaluate({}); // Throws
55+
```
56+
57+
The `Error` contains information about the nature of the run-time error, for example:
58+
59+
```
60+
{
61+
code: "T1006",
62+
stack: "...",
63+
position: 14,
64+
token: "notafunction",
65+
message: "Attempted to invoke a non-function"
66+
}
67+
```
68+
69+
If `callback(err, value)` is supplied, `expression.evaluate()` returns `undefined`, the expression is run asynchronously and the `Error` or result is passed to `callback`.
70+
71+
```javascript
72+
await jsonata("7 + 12").evaluate({}, {}, (error, result) => {
73+
if(error) {
74+
console.error(error);
75+
return;
76+
}
77+
console.log("Finished with", result);
78+
});
79+
console.log("Started");
80+
81+
// Prints "Started", then "Finished with 19"
82+
```
83+
84+
### expression.assign(name, value)
85+
86+
Permanently binds a value to a name in the expression, similar to how `bindings` worked above. Modifies `expression` in place and returns `undefined`. Useful in a JSONata expression factory.
87+
88+
```javascript
89+
var expression = jsonata("$a + $b()");
90+
expression.assign("a", 4);
91+
expression.assign("b", () => 1);
92+
93+
await expression.evaluate({}); // 5
94+
```
95+
96+
Note that the `bindings` argument in the `expression.evaluate()` call clobbers these values:
97+
98+
```javascript
99+
await expression.evaluate({}, {a: 109}); // 110
100+
```
101+
102+
### expression.registerFunction(name, implementation[, signature])
103+
104+
Permanently binds a function to a name in the expression.
105+
106+
```javascript
107+
var expression = jsonata("$greet()");
108+
expression.registerFunction("greet", () => "Hello world");
109+
110+
await expression.evaluate({}); // "Hello world"
111+
```
112+
113+
You can do this using `expression.assign` or `bindings` in `expression.evaluate`, but `expression.registerFunction` allows you to specify a function `signature`. This is a terse string which tells JSONata the expected input argument types and return value type of the function. JSONata raises a run-time error if the actual input argument types do not match (the return value type is not checked yet).
114+
115+
```javascript
116+
var expression = jsonata("$add(61, 10005)");
117+
expression.registerFunction("add", (a, b) => a + b, "<nn:n>");
118+
119+
await expression.evaluate({}); // 10066
120+
```
121+
122+
### Function signature syntax
123+
124+
A function signature is a string of the form `<params:return>`. `params` is a sequence of type symbols, each one representing an input argument's type. `return` is a single type symbol representing the return value type.
125+
126+
Type symbols work as follows:
127+
128+
Simple types:
129+
130+
- `b` - Boolean
131+
- `n` - number
132+
- `s` - string
133+
- `l` - `null`
134+
135+
Complex types:
136+
137+
- `a` - array
138+
- `o` - object
139+
- `f` - function
140+
141+
Union types:
142+
143+
- `(sao)` - string, array or object
144+
- `(o)` - same as `o`
145+
- `u` - equivalent to `(bnsl)` i.e. Boolean, number, string or `null`
146+
- `j` - any JSON type. Equivalent to `(bnsloa)` i.e. Boolean, number, string, `null`, object or array, but not function
147+
- `x` - any type. Equivalent to `(bnsloaf)`
148+
149+
Parametrised types:
150+
151+
- `a<s>` - array of strings
152+
- `a<x>` - array of values of any type
153+
154+
Some examples of signatures of built-in JSONata functions:
155+
156+
- `$count` has signature `<a:n>`; it accepts an array and returns a number.
157+
- `$append` has signature `<aa:a>`; it accepts two arrays and returns an array.
158+
- `$sum` has signature `<a<n>:n>`; it accepts an array of numbers and returns a number.
159+
- `$reduce` has signature `<fa<j>:j>`; it accepts a reducer function `f` and an `a<j>` (array of JSON objects) and returns a JSON object.
160+
161+
Each type symbol may also have *options* applied.
162+
163+
- `+` - one or more arguments of this type
164+
- E.g. `$zip` has signature `<a+>`; it accepts one array, or two arrays, or three arrays, or...
165+
- `?` - optional argument
166+
- E.g. `$join` has signature `<a<s>s?:s>`; it accepts an array of strings and an optional joiner string which defaults to the empty string. It returns a string.
167+
- `-` - if this argument is missing, use the context value ("focus").
168+
- E.g. `$length` has signature `<s-:n>`; it can be called as `$length(OrderID)` (one argument) but equivalently as `OrderID.$length()`.
169+
170+
### Writing higher-order function extensions
171+
172+
It is possible to write an extension function that takes one or more functions in its list of arguments and/or returns
173+
a function as its return value.
174+
175+

0 commit comments

Comments
 (0)