You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var json =JSON.parse(document.getElementById('json').value);
@@ -58,175 +60,11 @@ In a browser:
58
60
</html>
59
61
```
60
62
61
-
`jsonata` uses ES2015 features such as [generators](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/function*). For browsers lacking these features, `lib/jsonata-es5.js` is provided.
62
-
63
-
## API
64
-
65
-
### jsonata(str)
66
-
67
-
Parse a string `str` as a JSONata expression and return a compiled JSONata expression object.
68
-
69
-
```javascript
70
-
var expression =jsonata("$sum(example.value)");
71
-
```
72
-
73
-
If the expression is not valid JSONata, an `Error` is thrown containing information about the nature of the syntax error, for example:
Run the compiled JSONata expression against object `input` and return the result as a new object.
91
-
92
-
```javascript
93
-
var result =expression.evaluate({example: [{value:4}, {value:7}, {value:13}]});
94
-
```
95
-
96
-
`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.
97
-
98
-
`bindings`, if present, contains variable names and values (including functions) to be bound:
`expression.evaluate()` may throw a run-time `Error`:
106
-
107
-
```javascript
108
-
var expression =jsonata("$notafunction()"); // OK, valid JSONata
109
-
expression.evaluate({}); // Throws
110
-
```
111
-
112
-
The `Error` contains information about the nature of the run-time error, for example:
113
-
114
-
```
115
-
{
116
-
code: "T1006",
117
-
stack: "...",
118
-
position: 14,
119
-
token: "notafunction",
120
-
message: "Attempted to invoke a non-function"
121
-
}
122
-
```
123
-
124
-
If `callback(err, value)` is supplied, `expression.evaluate()` returns `undefined`, the expression is run asynchronously and the `Error` or result is passed to `callback`.
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.
142
-
143
-
```javascript
144
-
var expression =jsonata("$a + $b()");
145
-
expression.assign("a", 4);
146
-
expression.assign("b", () =>1);
147
-
148
-
expression.evaluate({}); // 5
149
-
```
150
-
151
-
Note that the `bindings` argument in the `expression.evaluate()` call clobbers these values:
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).
169
-
170
-
```javascript
171
-
var expression =jsonata("$add(61, 10005)");
172
-
expression.registerFunction("add", (a, b) => a + b, "<nn:n>");
173
-
174
-
expression.evaluate({}); // 10066
175
-
```
176
-
177
-
Function signatures are specified like so:
178
-
179
-
##### Function signature syntax
180
-
181
-
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.
182
-
183
-
Type symbols work as follows:
184
-
185
-
Simple types:
186
-
187
-
-`b` - Boolean
188
-
-`n` - number
189
-
-`s` - string
190
-
-`l` - `null`
191
-
192
-
Complex types:
193
-
194
-
-`a` - array
195
-
-`o` - object
196
-
-`f` - function
197
-
198
-
Union types:
199
-
200
-
-`(sao)` - string, array or object
201
-
-`(o)` - same as `o`
202
-
-`u` - equivalent to `(bnsl)` i.e. Boolean, number, string or `null`
203
-
-`j` - any JSON type. Equivalent to `(bnsloa)` i.e. Boolean, number, string, `null`, object or array, but not function
204
-
-`x` - any type. Equivalent to `(bnsloaf)`
205
-
206
-
Parametrised types:
207
-
208
-
-`a<s>` - array of strings
209
-
-`a<x>` - array of values of any type
210
-
211
-
Some examples of signatures of built-in JSONata functions:
212
-
213
-
-`$count` has signature `<a:n>`; it accepts an array and returns a number.
214
-
-`$append` has signature `<aa:a>`; it accepts two arrays and returns an array.
215
-
-`$sum` has signature `<a<n>:n>`; it accepts an array of numbers and returns a number.
216
-
-`$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.
217
-
218
-
Each type symbol may also have *options* applied.
219
-
220
-
-`+` - one or more arguments of this type
221
-
- E.g. `$zip` has signature `<a+>`; it accepts one array, or two arrays, or three arrays, or...
222
-
-`?` - optional argument
223
-
- 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.
224
-
-`-` - if this argument is missing, use the context value ("focus").
225
-
- E.g. `$length` has signature `<s-:n>`; it can be called as `$length(OrderID)` (one argument) but equivalently as `OrderID.$length()`.
0 commit comments