Skip to content

Commit eb1033b

Browse files
andrew-colemanmattbaileyuk
authored andcommitted
simplify readme
1 parent cb78299 commit eb1033b

File tree

1 file changed

+11
-173
lines changed

1 file changed

+11
-173
lines changed

README.md

+11-173
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
# jsonata
1+
# JSONata
2+
3+
JSON query and transformation language
24

35
[![NPM statistics](https://nodei.co/npm/jsonata.png?downloads=true&downloadRank=true)](https://nodei.co/npm/jsonata/)
46

57
[![Build Status](https://travis-ci.org/jsonata-js/jsonata.svg)](https://travis-ci.org/jsonata-js/jsonata)
68
[![Coverage Status](https://coveralls.io/repos/github/jsonata-js/jsonata/badge.svg?branch=master)](https://coveralls.io/github/jsonata-js/jsonata?branch=master)
79

8-
JavaScript implementation of the [JSONata query and transformation language](http://jsonata.org/).
10+
Reference implementation of the [JSONata query and transformation language](http://jsonata.org/).
911

10-
* [JSONata tutorial](tutorial.md)
12+
* [JSONata in 5 minutes](https://www.youtube.com/embed/ZBaK40rtIBM)
1113
* [JSONata language documentation](http://docs.jsonata.org/)
1214
* [Try it out!](http://try.jsonata.org/)
1315

1416
## Installation
1517

1618
- `npm install jsonata`
1719

18-
## Usage
20+
## Quick start
1921

2022
In Node.js:
2123

@@ -41,7 +43,7 @@ In a browser:
4143
<head>
4244
<meta charset="UTF-8">
4345
<title>JSONata test</title>
44-
<script src="lib/jsonata.js"></script>
46+
<script src="https://cdn.jsdelivr.net/npm/jsonata/jsonata.min.js"></script>
4547
<script>
4648
function greeting() {
4749
var json = JSON.parse(document.getElementById('json').value);
@@ -58,175 +60,11 @@ In a browser:
5860
</html>
5961
```
6062

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:
74-
75-
```
76-
{
77-
code: "S0202",
78-
stack: "...",
79-
position: 16,
80-
token: "}",
81-
value: "]",
82-
message: "Syntax error: expected ']' got '}'"
83-
}
84-
```
85-
86-
`expression` has three methods:
87-
88-
#### expression.evaluate(input[, bindings[, callback]])
89-
90-
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:
99-
100-
```javascript
101-
jsonata("$a + $b()").evaluate({}, {a: 4, b: () => 78});
102-
// returns 82
103-
```
104-
105-
`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`.
125-
126-
```javascript
127-
jsonata("7 + 12").evaluate({}, {}, (error, result) => {
128-
if(error) {
129-
console.error(error);
130-
return;
131-
}
132-
console.log("Finished with", result);
133-
});
134-
console.log("Started");
135-
136-
// Prints "Started", then "Finished with 19"
137-
```
138-
139-
#### expression.assign(name, value)
140-
141-
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:
152-
153-
```javascript
154-
expression.evaluate({}, {a: 109}); // 110
155-
```
156-
157-
#### expression.registerFunction(name, implementation[, signature])
158-
159-
Permanently binds a function to a name in the expression.
160-
161-
```javascript
162-
var expression = jsonata("$greet()");
163-
expression.registerFunction("greet", () => "Hello world");
164-
165-
expression.evaluate({}); // "Hello world"
166-
```
167-
168-
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()`.
226-
22763
## More information
228-
- JSONata [language documentation](http://docs.jsonata.org/)
229-
- JSONata [tech talk](https://developer.ibm.com/open/videos/dw-open-tech-talk-jsonata/)
64+
- JSONata [documentation](http://docs.jsonata.org/)
65+
- [JavaScript API](http://docs.jsonata.org/embedding-extending)
66+
- [Intro talk](https://www.youtube.com/watch?v=TDWf6R8aqDo) at London Node User Group
67+
- JSONata [tech talk](https://www.youtube.com/watch?v=ZRtlkIj0uDY)
23068

23169
## Contributing
23270

0 commit comments

Comments
 (0)