conditional-expressions-json
v0.1.4
Published
Library for evaluating expressions in JSON data.
Downloads
1
Readme
conditional-expressions-json
Library for evaluating expressions in JSON data. Enables conditional section in your JSON data based on a provided context.
Example
{
"House": {
"Doors": 1,
"Windows: "[$.windows]",
"Basement": {
"$if[$.basementRooms > 0]": {
"Rooms": "[$.basementRooms]"
}
}
}
}
import { applyConditionals } from 'conditional-expressions-json';
let data = JSON.parse(text);
let evaluated = applyConditionals(data, { windows: 4, basementRooms: 2 });
// {
// "House": {
// "Doors": 1,
// "Windows": 4,
// "Basement": {
// "Rooms": 2
// }
// }
// }
See unit tests for more examples.
Expression language
The language is fairly simple:
- Lazy logical operators
&&
and||
- Comparison operators
==
,!=
,<
,>
,<=
,>=
- Function application
f(e1, e2, ...)
- Context access
$.a.b[0]
- Constants
12
,'Abc'
,true
Built-in functions:
date(string)
- converts a string into a datenot(bool)
- inverts a boolean valueiff(bool, ifTrue, ifFalse)
- lazy conditional function
Contributions welcome for more useful functions.