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
evaluates to 3.
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, ['$', '/foo']]; const data = {foo: 2}; evaluate(expression, {data}); // 3
Pre-compiling expression to an optimized function.
import {JsonExpressionCodegen} from '@jsonjoy.com/json-expression'; const expression = ['+', 1, ['$', '/foo']]; const codegen = new JsonExpressionCodegen({expression}); const fn = codegen.run().compile(); const data = {foo: 2}; fn({data}); // 3
json-expression
library supports few dozen operators, see full list in Expr
type here.
Parsing rules:
["get", "/some/path"]
.[[]]
. This evaluates to an empty array JSON value []
.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', ['==', ['$', '/specversion'], '1.0'], ['starts', ['$', '/type'], 'com.example.'], ['in', ['$', '/datacontenttype'], [['application/octet-stream', 'application/json']]], ['==', ['$', '/data/appinfoA'], 'abc'], ];
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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4