jexp
v1.0.5
Published
Parser and evaluator of expressions by line of commands. The expressions correspond to the package 3xpr
Maintainers
Readme
JEXP
Command line application that allows parser and evaluator of json/yaml applying expressions.
Features
- json and yaml formats
- Constants, enums, number, string, datetime, variables, objects and array
- Arithmetic , assignment , comparison , logical and bitwise operators
- Number , string , datetime , array and nullable functions
- Conversion functions
- Arrow functions
- Group functions (distinct, first, last, min, max, sum and avg)
- Sets functions (union, intersection, difference and symmetric difference)
- Environment variables
Global installation
it is necessary to install the package globally to be able to access the command line applications.
npm install jexp -gUsage
Commands
| Command | Description | |-----------|-----------------------------------------------------| | eval | returns the result of the expression on the source | | validate | validate the source from a schema |
Eval:
jexp eval <expression> <source> [options]Validate:
jexp validate <schema> <source> [options]Expression
The expressions correspond to the package 3xpr expression that is applied to the data source
The root of the data is accessed from dot
jexp eval '.' ./data/orders.jsonFrom the dot we write the expressions
jexp eval '.details.article' ./data/orders.jsonjexp eval '.details.first(p=> p.unitPrice * p.qty < 3 )' ./data/orders.jsonSource
Get data source from json file
jexp eval '.[0].details' ./data/orders.jsonGet data source from yaml file
jexp eval '.min(p=> p.total)' ./data/orders.yamlGet data source from json stringify
jexp eval 'concatenate(capitalize(.fruit.name)," ",.fruit.color)' '{"fruit":{"name":"apple","color":"green","price":1.20}}'Get data source from pipeline command
curl -s https://raw.githubusercontent.com/data7expressions/jexp/main/data/orders.json | jexp eval '.number'Options
| Option | Abbreviation | Description | Options | |-------------------|--------------|----------------|-----------| |--output |-o |Force output | json, yaml| |--beautiful |-b |Beautiful output| |
Examples
file orders.js
[
{
"number": "20001",
"customer": { "firstName": "John", "lastName": "Murphy" },
"orderTime": "2022-07-30T10:15:54",
"details": [
{ "article": "Pear", "unitPrice": 1.78, "qty": 2 },
{ "article": "Banana", "unitPrice": 1.99, "qty": 1 },
{ "article": "White grape", "unitPrice": 2.03, "qty": 1 }
]
},
{
"number": "20002",
"customer": { "firstName": "Paul", "lastName": "Smith" },
"orderTime": "2022-07-30T12:12:43",
"details": [
{ "article": "Apple", "unitPrice": 2.15, "qty": 1 },
{ "article": "Banana", "unitPrice": 1.99, "qty": 2 },
{ "article": "Pear", "unitPrice": 1.78, "qty": 1 }
]
},
{
"number": "20003",
"customer": { "firstName": "George", "lastName": "Williams" },
"orderTime": "2022-07-30T14:43:11",
"details": [
{ "article": "Apple", "unitPrice": 2.15, "qty": 1 },
{ "article": "Banana", "unitPrice": 1.99, "qty": 1 },
{ "article": "Pear", "unitPrice": 1.78, "qty": 1 },
{ "article": "White grape", "unitPrice": 2.03, "qty": 1 }
]
}
]Return the entire content of the file:
jexp eval '.' ./data/orders.jsonResult:
[{"number":"20001","customer":{"firstName":"John","lastName":"Murphy"},"orderTime":"2022-07-30T10:15:54","details":[{"article":"Pear","unitPrice":1.78,"qty":2},{"article":"Banana","unitPrice":1.99,"qty":1},{"article":"White grape","unitPrice":2.03,"qty":1}]},{"number":"20002","customer":{"firstName":"Paul","lastName":"Smith"},"orderTime":"2022-07-30T12:12:43","details":[{"article":"Apple","unitPrice":2.15,"qty":1},{"article":"Banana","unitPrice":1.99,"qty":2},{"article":"Pear","unitPrice":1.78,"qty":1}]},{"number":"20003","customer":{"firstName":"George","lastName":"Williams"},"orderTime":"2022-07-30T14:43:11","details":[{"article":"Apple","unitPrice":2.15,"qty":1},{"article":"Banana","unitPrice":1.99,"qty":1},{"article":"Pear","unitPrice":1.78,"qty":1},{"article":"White grape","unitPrice":2.03,"qty":1}]}]Returns the number property of the list:
jexp eval '.number' ./data/orders.jsonResult:
["20001","20002","20003"]Concatenates two properties and capitalizes the first one:
jexp eval 'concatenate(capitalize(.fruit.name)," ",.fruit.color)' '{"fruit":{"name":"apple","color":"green","price":1.20}}'Result:
"Apple green"Returns the first element of an array from the index in yaml format:
jexp eval '.[0]' ./data/orders.json -o yamlResult:
number: '20001'
customer:
firstName: John
lastName: Murphy
orderTime: '2022-07-30T10:15:54'
details:
- article: Pear
unitPrice: 1.78
qty: 2
- article: Banana
unitPrice: 1.99
qty: 1
- article: White grape
unitPrice: 2.03
qty: 1
Returns the details property of the first element in beautiful format:
jexp eval '.[0].details' ./data/orders.json -bResult:
[
{
"article": "Pear",
"unitPrice": 1.78,
"qty": 2
},
{
"article": "Banana",
"unitPrice": 1.99,
"qty": 1
},
{
"article": "White grape",
"unitPrice": 2.03,
"qty": 1
}
]Returns the details property of the first element, as the file is yaml, it returns it in yaml format:
jexp eval '.[0].details' ./data/orders.yamlResult:
- article: Pear
unitPrice: 1.78
qty: 2
- article: Banana
unitPrice: 1.99
qty: 1
- article: White grape
unitPrice: 2.03
qty: 1
Returns the details property of the first element, although the file is yaml it forces the output in json format:
jexp eval '.[0].details' ./data/orders.yaml -b -o jsonResult:
[
{
"article": "Pear",
"unitPrice": 1.78,
"qty": 2
},
{
"article": "Banana",
"unitPrice": 1.99,
"qty": 1
},
{
"article": "White grape",
"unitPrice": 2.03,
"qty": 1
}
]Returns the details property of the listing:
jexp eval '.details' ./data/orders.jsonResult:
[{"article":"Pear","unitPrice":1.78,"qty":2},{"article":"Banana","unitPrice":1.99,"qty":1},{"article":"White grape","unitPrice":2.03,"qty":1},{"article":"Apple","unitPrice":2.15,"qty":1},{"article":"Banana","unitPrice":1.99,"qty":2},{"article":"Pear","unitPrice":1.78,"qty":1},{"article":"Apple","unitPrice":2.15,"qty":1},{"article":"Banana","unitPrice":1.99,"qty":1},{"article":"Pear","unitPrice":1.78,"qty":1},{"article":"White grape","unitPrice":2.03,"qty":1}]Returns the article property of the list of details of each element of the list:
jexp eval '.details.article' ./data/orders.jsonResult:
["Pear","Banana","White grape","Apple","Banana","Pear","Apple","Banana","Pear","White grape"]The order with the smallest total:
jexp eval '.map(p=>{nro:p.number,total:p.details.sum(q=> q.unitPrice * q.qty)}).min(p=> p.total)' ./data/orders.jsonResult:
7.58Get the minimum of the article property from all the details:
jexp eval '.details.min(p=> p.article )' ./data/orders.jsonResult:
"Apple"Get the maximum "unitPrice * p.qty" from all the details:
jexp eval '.details.max(p=> p.unitPrice * p.qty )' ./data/orders.jsonResult:
3.98Get the middle value "unitPrice * p.qty" from all the details:
jexp eval 'round(.details.avg(p=> p.unitPrice * p.qty),2)' ./data/orders.jsonResult:
2.35Gets the sum of the total property:
jexp eval '.sum(p=> p.total )' ./data/orders.jsonResult:
0Get the sum "unitPrice * p.qty" of the details of item 1 of the list:
jexp eval '.[1].details.sum(p=> p.unitPrice * p.qty )' ./data/orders.jsonResult:
7.91Get the number of details where "unitPrice * p.qty " is less than 3:
jexp eval '.details.count(p=> p.unitPrice * p.qty < 3 )' ./data/orders.jsonResult:
8Get the first article property of all details where "unitPrice * p.qty" is less than 3:
jexp eval '.details.first(p=> p.unitPrice * p.qty < 3 ).article' ./data/orders.jsonResult:
"Banana"Get the last article property of all details where "unitPrice * p.qty" is less than 3:
jexp eval '.details.last(p=> p.unitPrice * p.qty < 3 ).article' ./data/orders.jsonResult:
"White grape"Get the first detail where "unitPrice * p.qty" is less than 3 in beautiful format:
jexp eval '.details.first(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json -bResult:
{
"article": "Banana",
"unitPrice": 1.99,
"qty": 1
}Get the smallest article:
jexp eval '.details.min(p=> p.article )' ./data/orders.jsonResult:
"Apple"Get the total of all the details:
jexp eval '.details.max(p=> p.unitPrice * p.qty )' ./data/orders.jsonResult:
3.98Average value of the price of the items purchased in the order 20003:
jexp eval 'round(.filter(p=> p.number == "20003").details.avg(p=> p.unitPrice),2)' ./data/orders.jsonResult:
2Get the total of the details of order 1:
jexp eval '.[1].details.sum(p=> p.unitPrice * p.qty )' ./data/orders.jsonResult:
7.91Gets the number of details where the subtotal is less than 3:
jexp eval '.details.count(p=> p.unitPrice * p.qty < 3 )' ./data/orders.jsonResult:
8Get the article of the first detail where the subtotal is less than 3:
jexp eval '.details.first(p=> p.unitPrice * p.qty < 3 ).article' ./data/orders.jsonResult:
"Banana"Get the article of the last detail where the subtotal is less than 3:
jexp eval '.details.last(p=> p.unitPrice * p.qty < 3 ).article' ./data/orders.jsonResult:
"White grape"Get the first detail where the subtotal is less than 3:
jexp eval '.details.first(p=> p.unitPrice * p.qty < 3 )' ./data/orders.jsonResult:
{"article":"Banana","unitPrice":1.99,"qty":1}Calculate the total for each order:
jexp eval '.each(p=>p.total=p.details.sum(q=>q.qty*q.unitPrice)).map(p=>{nro:p.number,total:p.total})' ./data/orders.jsonResult:
[{"nro":"20001","total":7.58},{"nro":"20002","total":7.91},{"nro":"20003","total":7.949999999999999}]Calculate the subtotal for each order:
jexp eval '.details.foreach(p=>p.subtotal=p.qty*p.unitPrice).subtotal' ./data/orders.jsonResult:
[3.56,1.99,2.03,2.15,3.98,1.78,2.15,1.99,1.78,2.03]calculates the total of all the details:
jexp eval '.details.foreach(p=>total=nvl(total,0)+p.qty*p.unitPrice);total' ./data/orders.jsonResult:
23.44Get the list of items without repeating:
jexp eval '.details.distinct(p=>p.article)' ./data/orders.json -bResult:
[
"Pear",
"Banana",
"White grape",
"Apple"
]Get the total and amount of each item:
jexp eval '.details.map(p=>{article:p.article,count:count(1),total:sum(p.qty * p.unitPrice)})' ./data/orders.json -bResult:
[
{
"article": "Pear",
"count": 3,
"total": 7.12
},
{
"article": "Banana",
"count": 3,
"total": 7.96
},
{
"article": "White grape",
"count": 2,
"total": 4.06
},
{
"article": "Apple",
"count": 2,
"total": 4.3
}
]Get the total of the first order:
jexp eval '{total:.[0].details.sum(p=>p.qty * p.unitPrice)}' ./data/orders.jsonResult:
{"total":7.58}Get the total of the last order:
jexp eval '{total:round(.[.length()-1].details.sum(p=>p.qty * p.unitPrice),2)}' ./data/orders.json -bResult:
{
"total": 7.95
}List the orders with their totals:
jexp eval '.map(p=>{nro:p.number,total:round(p.details.sum(q=>q.qty * q.unitPrice),2)})' ./data/orders.json -bResult:
[
{
"nro": "20001",
"total": 7.6
},
{
"nro": "20002",
"total": 7.9
},
{
"nro": "20003",
"total": 7.95
}
]All articles that are in orders 20001 and 20003:
jexp eval '.[0].details.article.union(.[1].details.article)' ./data/orders.jsonResult:
["Pear","Banana","White grape","Apple"]The articles in common between order 20001 and 20002:
jexp eval '.[0].details.article.intersection(.[1].details.article)' ./data/orders.jsonResult:
["Banana","Pear"]Articles that are in order 20001 and are not in order 20002:
jexp eval '.[0].details.article.difference(.[1].details.article)' ./data/orders.jsonResult:
["White grape"]Articles of orders 20001 and 20003 that are not shared:
jexp eval '.[0].details.article.symmetricDifference(.[1].details.article)' ./data/orders.jsonResult:
["White grape","Apple"]Get the sum "unitPrice * p.qty" of the details of item 1 of the list using pipeline:
curl -s https://raw.githubusercontent.com/data7expressions/jexp/main/data/orders.json | jexp eval '.details.sum(p=> p.unitPrice * p.qty )'Result:
23.44js-expression
Js-expression is an extensible expression evaluator and parser.
Besides the operators, functions, variables, objects and arrays that are supported.
