Skip to content

markuskimius/jsonexpr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSONexpr

License Release clang17 python38 javascript

A safe, language-neutral expression language built on JSON.

Motivation

Quite simply, I needed a way to pass between processes a code snippet that can be evaluated safely, by any process regardless of the language the process was writen in, with an identical behavior across the languages.

So I made one.

And it just so happened that using JSON as the datatype of this language was very useful.

Basic Examples

Mathematical expressions are valid:

$ je -e '1 + 2 + 3 + 4 + 5 + 6 + 7 + 8'
36
$

You can use variables:

$ je -e 'n=8; n * (n+1) / 2'
36
$

A variable can store any valid JSON value:

$ je -e 'myvar = { "n" : 8 }; myvar.n * (myvar.n+1) / 2'
36
$

You can have loops:

$ je -e 'sum=0; FOR(i=0, i<9, i++, sum+=i); sum'
36
$

You can have functions:

$ je -e 'SQRT(36)'
6.0
$

Above examples evaluate JSONexpr expression from the command line using the je command that comes with the C and Python installations of JSONexpr, but JSONexpr is primarily designed to be evaluated programmatically. See documentation for the list of supported programming languages that can evaluate JSONexpr expressions today; additional language support can be added with a WASM runtime.

More Examples

Since a value is a valid expression in an expression language, JSON is a valid expression in JSONexpr. And you can embed expressions in the JSON. For example:

$ cat students.je
[
  {
    "name": "Alice",
    "score": (95 + 98 + 94) / 3.0
  },
  {
    "name": "Bob",
    "score": (85 + 88 + 84) / 3.0
  },
  {
    "name": "Charlie",
    "score": (75 + 78 + 74) / 3.0
  }
]
$ je students.je | jq .
[
  {
    "name": "Alice",
    "score": 95.66666666666667
  },
  {
    "name": "Bob",
    "score": 85.66666666666667
  },
  {
    "name": "Charlie",
    "score": 75.66666666666667
  }
]
$

Demo

Interactive demo here

Documentation

License

Apache 2.0

About

An expression language built on JSON.

Resources

License

Stars

Watchers

Forks

Packages

No packages published