JSON Expression
JSON Expression is an s-expression based language for JSON. It is designed to be a more human-readable and writable alternative to JSON. It uses JSON as its data model and syntax.
JSON Expressions are JIT compiled to efficient machine code.
JSON Expression is a simple JSON DSL, which allows to write expressions and evaluate expressions.
For example, the following expression
['+', 1, 2]; // 1 + 2
evaluates to 3.
Usage
json-expression library can immediately evaluate expressions or it can
compile an efficient expression to a function, which will execute about
an order of magnitude faster.
Evaluating expression immediately as-is.
import {evaluate} from '@jsonjoy.com/json-expression';
const expression = ['+', 1, ['
Pre-compiling expression to an optimized function.
import {JsonExpressionCodegen} from '@jsonjoy.com/json-expression';
const expression = ['+', 1, ['
Documentation
json-expression library supports few dozen operators, see full list in Expr
type here.
Parsing rules:
- JSON Expression is a valid JSON value.
- All expressions are JSON arrays, which start with a string which specifies
the operator and remaining array elements are operands. For example, the
"get" operator fetches some value from supplied data using JSON
Pointer:
["get", "/some/path"].
- All other values are treated as literals. Except for arrays, which need to
be enclosed in square brackets. For example, to specify an empty array, you
box your array in square brackets:
[[]]. This evaluates to an empty array
JSON value [].
Use Cases
Consider you application receives a stream of JSON Cloud Events, like this:
{
"specversion" : "1.0",
"type" : "com.example.someevent",
"source" : "/mycontext",
"subject": null,
"id" : "C234-1234-1234",
"time" : "2018-04-05T17:31:00Z",
"comexampleextension1" : "value",
"comexampleothervalue" : 5,
"datacontenttype" : "application/json",
"data" : {
"appinfoA" : "abc",
"appinfoB" : 123,
"appinfoC" : true
}
}
You could write and compile a JSON Expression to efficiently filter out events
you are interested in, for example your expression could look like this:
[
'and',
['==', ['
Benchmark
node benchmarks/json-expression/main.js
json-joy/json-expression JsonExpressionCodegen x 14,557,786 ops/sec ±0.09% (100 runs sampled), 69 ns/op
json-joy/json-expression JsonExpressionCodegen with codegen x 170,098 ops/sec ±0.13% (101 runs sampled), 5879 ns/op
json-joy/json-expression evaluate x 864,956 ops/sec ±0.10% (101 runs sampled), 1156 ns/op
json-logic-js x 821,799 ops/sec ±0.18% (99 runs sampled), 1217 ns/op
Fastest is json-joy/json-expression JsonExpressionCodegen
, '/foo']];
const data = {foo: 2};
evaluate(expression, {data}); // 3
Pre-compiling expression to an optimized function.
__CODE_BLOCK_2__
Documentation
__INLINE_CODE_1__ library supports few dozen operators, see full list in __INLINE_CODE_2__
type here.
Parsing rules:
- JSON Expression is a valid JSON value.
- All expressions are JSON arrays, which start with a string which specifies
the operator and remaining array elements are operands. For example, the
"get" operator fetches some value from supplied data using JSON
Pointer:__INLINE_CODE_3__.
- All other values are treated as literals. Except for arrays, which need to
be enclosed in square brackets. For example, to specify an empty array, you
box your array in square brackets: __INLINE_CODE_4__. This evaluates to an empty array
JSON value __INLINE_CODE_5__.
Use Cases
Consider you application receives a stream of JSON Cloud Events, like this:
__CODE_BLOCK_3__
You could write and compile a JSON Expression to efficiently filter out events
you are interested in, for example your expression could look like this:
__CODE_BLOCK_4__
Benchmark
__CODE_BLOCK_5__
, '/foo']];
const codegen = new JsonExpressionCodegen({expression});
const fn = codegen.run().compile();
const data = {foo: 2};
fn({data}); // 3
Documentation
__INLINE_CODE_1__ library supports few dozen operators, see full list in __INLINE_CODE_2__
type here.
Parsing rules:
- JSON Expression is a valid JSON value.
- All expressions are JSON arrays, which start with a string which specifies
the operator and remaining array elements are operands. For example, the
"get" operator fetches some value from supplied data using JSON
Pointer:__INLINE_CODE_3__.
- All other values are treated as literals. Except for arrays, which need to
be enclosed in square brackets. For example, to specify an empty array, you
box your array in square brackets: __INLINE_CODE_4__. This evaluates to an empty array
JSON value __INLINE_CODE_5__.
Use Cases
Consider you application receives a stream of JSON Cloud Events, like this:
__CODE_BLOCK_3__
You could write and compile a JSON Expression to efficiently filter out events
you are interested in, for example your expression could look like this:
__CODE_BLOCK_4__
Benchmark
__CODE_BLOCK_5__
, '/foo']];
const data = {foo: 2};
evaluate(expression, {data}); // 3
Pre-compiling expression to an optimized function.
__CODE_BLOCK_2__Documentation
__INLINE_CODE_1__ library supports few dozen operators, see full list in __INLINE_CODE_2__ type here.
Parsing rules:
- JSON Expression is a valid JSON value.
- All expressions are JSON arrays, which start with a string which specifies the operator and remaining array elements are operands. For example, the "get" operator fetches some value from supplied data using JSON Pointer:__INLINE_CODE_3__.
- All other values are treated as literals. Except for arrays, which need to be enclosed in square brackets. For example, to specify an empty array, you box your array in square brackets: __INLINE_CODE_4__. This evaluates to an empty array JSON value __INLINE_CODE_5__.
Use Cases
Consider you application receives a stream of JSON Cloud Events, like this:
__CODE_BLOCK_3__You could write and compile a JSON Expression to efficiently filter out events you are interested in, for example your expression could look like this:
__CODE_BLOCK_4__Benchmark
__CODE_BLOCK_5__ , '/specversion'], '1.0'], ['starts', ['Benchmark
__CODE_BLOCK_5__ , '/foo']]; const data = {foo: 2}; evaluate(expression, {data}); // 3Pre-compiling expression to an optimized function.
__CODE_BLOCK_2__Documentation
__INLINE_CODE_1__ library supports few dozen operators, see full list in __INLINE_CODE_2__ type here.
Parsing rules:
- JSON Expression is a valid JSON value.
- All expressions are JSON arrays, which start with a string which specifies the operator and remaining array elements are operands. For example, the "get" operator fetches some value from supplied data using JSON Pointer:__INLINE_CODE_3__.
- All other values are treated as literals. Except for arrays, which need to be enclosed in square brackets. For example, to specify an empty array, you box your array in square brackets: __INLINE_CODE_4__. This evaluates to an empty array JSON value __INLINE_CODE_5__.
Use Cases
Consider you application receives a stream of JSON Cloud Events, like this:
__CODE_BLOCK_3__You could write and compile a JSON Expression to efficiently filter out events you are interested in, for example your expression could look like this:
__CODE_BLOCK_4__Benchmark
__CODE_BLOCK_5__ , '/foo']]; const codegen = new JsonExpressionCodegen({expression}); const fn = codegen.run().compile(); const data = {foo: 2}; fn({data}); // 3Documentation
__INLINE_CODE_1__ library supports few dozen operators, see full list in __INLINE_CODE_2__ type here.
Parsing rules:
- JSON Expression is a valid JSON value.
- All expressions are JSON arrays, which start with a string which specifies the operator and remaining array elements are operands. For example, the "get" operator fetches some value from supplied data using JSON Pointer:__INLINE_CODE_3__.
- All other values are treated as literals. Except for arrays, which need to be enclosed in square brackets. For example, to specify an empty array, you box your array in square brackets: __INLINE_CODE_4__. This evaluates to an empty array JSON value __INLINE_CODE_5__.
Use Cases
Consider you application receives a stream of JSON Cloud Events, like this:
__CODE_BLOCK_3__You could write and compile a JSON Expression to efficiently filter out events you are interested in, for example your expression could look like this:
__CODE_BLOCK_4__Benchmark
__CODE_BLOCK_5__ , '/foo']]; const data = {foo: 2}; evaluate(expression, {data}); // 3Pre-compiling expression to an optimized function.
__CODE_BLOCK_2__Documentation
__INLINE_CODE_1__ library supports few dozen operators, see full list in __INLINE_CODE_2__ type here.
Parsing rules:
- JSON Expression is a valid JSON value.
- All expressions are JSON arrays, which start with a string which specifies the operator and remaining array elements are operands. For example, the "get" operator fetches some value from supplied data using JSON Pointer:__INLINE_CODE_3__.
- All other values are treated as literals. Except for arrays, which need to be enclosed in square brackets. For example, to specify an empty array, you box your array in square brackets: __INLINE_CODE_4__. This evaluates to an empty array JSON value __INLINE_CODE_5__.
Use Cases
Consider you application receives a stream of JSON Cloud Events, like this:
__CODE_BLOCK_3__You could write and compile a JSON Expression to efficiently filter out events you are interested in, for example your expression could look like this:
__CODE_BLOCK_4__Benchmark
__CODE_BLOCK_5__ , '/type'], 'com.example.'], ['in', ['Benchmark
__CODE_BLOCK_5__ , '/foo']]; const data = {foo: 2}; evaluate(expression, {data}); // 3Pre-compiling expression to an optimized function.
__CODE_BLOCK_2__Documentation
__INLINE_CODE_1__ library supports few dozen operators, see full list in __INLINE_CODE_2__ type here.
Parsing rules:
- JSON Expression is a valid JSON value.
- All expressions are JSON arrays, which start with a string which specifies the operator and remaining array elements are operands. For example, the "get" operator fetches some value from supplied data using JSON Pointer:__INLINE_CODE_3__.
- All other values are treated as literals. Except for arrays, which need to be enclosed in square brackets. For example, to specify an empty array, you box your array in square brackets: __INLINE_CODE_4__. This evaluates to an empty array JSON value __INLINE_CODE_5__.
Use Cases
Consider you application receives a stream of JSON Cloud Events, like this:
__CODE_BLOCK_3__You could write and compile a JSON Expression to efficiently filter out events you are interested in, for example your expression could look like this:
__CODE_BLOCK_4__Benchmark
__CODE_BLOCK_5__ , '/foo']]; const codegen = new JsonExpressionCodegen({expression}); const fn = codegen.run().compile(); const data = {foo: 2}; fn({data}); // 3Documentation
__INLINE_CODE_1__ library supports few dozen operators, see full list in __INLINE_CODE_2__ type here.
Parsing rules:
- JSON Expression is a valid JSON value.
- All expressions are JSON arrays, which start with a string which specifies the operator and remaining array elements are operands. For example, the "get" operator fetches some value from supplied data using JSON Pointer:__INLINE_CODE_3__.
- All other values are treated as literals. Except for arrays, which need to be enclosed in square brackets. For example, to specify an empty array, you box your array in square brackets: __INLINE_CODE_4__. This evaluates to an empty array JSON value __INLINE_CODE_5__.
Use Cases
Consider you application receives a stream of JSON Cloud Events, like this:
__CODE_BLOCK_3__You could write and compile a JSON Expression to efficiently filter out events you are interested in, for example your expression could look like this:
__CODE_BLOCK_4__Benchmark
__CODE_BLOCK_5__ , '/foo']]; const data = {foo: 2}; evaluate(expression, {data}); // 3Pre-compiling expression to an optimized function.
__CODE_BLOCK_2__Documentation
__INLINE_CODE_1__ library supports few dozen operators, see full list in __INLINE_CODE_2__ type here.
Parsing rules:
- JSON Expression is a valid JSON value.
- All expressions are JSON arrays, which start with a string which specifies the operator and remaining array elements are operands. For example, the "get" operator fetches some value from supplied data using JSON Pointer:__INLINE_CODE_3__.
- All other values are treated as literals. Except for arrays, which need to be enclosed in square brackets. For example, to specify an empty array, you box your array in square brackets: __INLINE_CODE_4__. This evaluates to an empty array JSON value __INLINE_CODE_5__.
Use Cases
Consider you application receives a stream of JSON Cloud Events, like this:
__CODE_BLOCK_3__You could write and compile a JSON Expression to efficiently filter out events you are interested in, for example your expression could look like this:
__CODE_BLOCK_4__Benchmark
__CODE_BLOCK_5__ , '/datacontenttype'], [['application/octet-stream', 'application/json']]], ['==', ['Benchmark
__CODE_BLOCK_5__ , '/foo']]; const data = {foo: 2}; evaluate(expression, {data}); // 3Pre-compiling expression to an optimized function.
__CODE_BLOCK_2__Documentation
__INLINE_CODE_1__ library supports few dozen operators, see full list in __INLINE_CODE_2__ type here.
Parsing rules:
- JSON Expression is a valid JSON value.
- All expressions are JSON arrays, which start with a string which specifies the operator and remaining array elements are operands. For example, the "get" operator fetches some value from supplied data using JSON Pointer:__INLINE_CODE_3__.
- All other values are treated as literals. Except for arrays, which need to be enclosed in square brackets. For example, to specify an empty array, you box your array in square brackets: __INLINE_CODE_4__. This evaluates to an empty array JSON value __INLINE_CODE_5__.
Use Cases
Consider you application receives a stream of JSON Cloud Events, like this:
__CODE_BLOCK_3__You could write and compile a JSON Expression to efficiently filter out events you are interested in, for example your expression could look like this:
__CODE_BLOCK_4__Benchmark
__CODE_BLOCK_5__ , '/foo']]; const codegen = new JsonExpressionCodegen({expression}); const fn = codegen.run().compile(); const data = {foo: 2}; fn({data}); // 3Documentation
__INLINE_CODE_1__ library supports few dozen operators, see full list in __INLINE_CODE_2__ type here.
Parsing rules:
- JSON Expression is a valid JSON value.
- All expressions are JSON arrays, which start with a string which specifies the operator and remaining array elements are operands. For example, the "get" operator fetches some value from supplied data using JSON Pointer:__INLINE_CODE_3__.
- All other values are treated as literals. Except for arrays, which need to be enclosed in square brackets. For example, to specify an empty array, you box your array in square brackets: __INLINE_CODE_4__. This evaluates to an empty array JSON value __INLINE_CODE_5__.
Use Cases
Consider you application receives a stream of JSON Cloud Events, like this:
__CODE_BLOCK_3__You could write and compile a JSON Expression to efficiently filter out events you are interested in, for example your expression could look like this:
__CODE_BLOCK_4__Benchmark
__CODE_BLOCK_5__ , '/foo']]; const data = {foo: 2}; evaluate(expression, {data}); // 3Pre-compiling expression to an optimized function.
__CODE_BLOCK_2__Documentation
__INLINE_CODE_1__ library supports few dozen operators, see full list in __INLINE_CODE_2__ type here.
Parsing rules:
- JSON Expression is a valid JSON value.
- All expressions are JSON arrays, which start with a string which specifies the operator and remaining array elements are operands. For example, the "get" operator fetches some value from supplied data using JSON Pointer:__INLINE_CODE_3__.
- All other values are treated as literals. Except for arrays, which need to be enclosed in square brackets. For example, to specify an empty array, you box your array in square brackets: __INLINE_CODE_4__. This evaluates to an empty array JSON value __INLINE_CODE_5__.
Use Cases
Consider you application receives a stream of JSON Cloud Events, like this:
__CODE_BLOCK_3__You could write and compile a JSON Expression to efficiently filter out events you are interested in, for example your expression could look like this:
__CODE_BLOCK_4__Benchmark
__CODE_BLOCK_5__ , '/data/appinfoA'], 'abc'], ];Benchmark
__CODE_BLOCK_5__ , '/foo']]; const data = {foo: 2}; evaluate(expression, {data}); // 3Pre-compiling expression to an optimized function.
__CODE_BLOCK_2__Documentation
__INLINE_CODE_1__ library supports few dozen operators, see full list in __INLINE_CODE_2__ type here.
Parsing rules:
- JSON Expression is a valid JSON value.
- All expressions are JSON arrays, which start with a string which specifies the operator and remaining array elements are operands. For example, the "get" operator fetches some value from supplied data using JSON Pointer:__INLINE_CODE_3__.
- All other values are treated as literals. Except for arrays, which need to be enclosed in square brackets. For example, to specify an empty array, you box your array in square brackets: __INLINE_CODE_4__. This evaluates to an empty array JSON value __INLINE_CODE_5__.
Use Cases
Consider you application receives a stream of JSON Cloud Events, like this:
__CODE_BLOCK_3__You could write and compile a JSON Expression to efficiently filter out events you are interested in, for example your expression could look like this:
__CODE_BLOCK_4__Benchmark
__CODE_BLOCK_5__ , '/foo']]; const codegen = new JsonExpressionCodegen({expression}); const fn = codegen.run().compile(); const data = {foo: 2}; fn({data}); // 3Documentation
__INLINE_CODE_1__ library supports few dozen operators, see full list in __INLINE_CODE_2__ type here.
Parsing rules:
- JSON Expression is a valid JSON value.
- All expressions are JSON arrays, which start with a string which specifies the operator and remaining array elements are operands. For example, the "get" operator fetches some value from supplied data using JSON Pointer:__INLINE_CODE_3__.
- All other values are treated as literals. Except for arrays, which need to be enclosed in square brackets. For example, to specify an empty array, you box your array in square brackets: __INLINE_CODE_4__. This evaluates to an empty array JSON value __INLINE_CODE_5__.
Use Cases
Consider you application receives a stream of JSON Cloud Events, like this:
__CODE_BLOCK_3__You could write and compile a JSON Expression to efficiently filter out events you are interested in, for example your expression could look like this:
__CODE_BLOCK_4__Benchmark
__CODE_BLOCK_5__ , '/foo']]; const data = {foo: 2}; evaluate(expression, {data}); // 3Pre-compiling expression to an optimized function.
__CODE_BLOCK_2__Documentation
__INLINE_CODE_1__ library supports few dozen operators, see full list in __INLINE_CODE_2__ type here.
Parsing rules:
- JSON Expression is a valid JSON value.
- All expressions are JSON arrays, which start with a string which specifies the operator and remaining array elements are operands. For example, the "get" operator fetches some value from supplied data using JSON Pointer:__INLINE_CODE_3__.
- All other values are treated as literals. Except for arrays, which need to be enclosed in square brackets. For example, to specify an empty array, you box your array in square brackets: __INLINE_CODE_4__. This evaluates to an empty array JSON value __INLINE_CODE_5__.
Use Cases
Consider you application receives a stream of JSON Cloud Events, like this:
__CODE_BLOCK_3__You could write and compile a JSON Expression to efficiently filter out events you are interested in, for example your expression could look like this:
__CODE_BLOCK_4__