jexp
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

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 -g

Usage

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.json

From the dot we write the expressions

jexp eval '.details.article' ./data/orders.json
jexp eval '.details.first(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json

Source

Get data source from json file

jexp eval '.[0].details' ./data/orders.json

Get data source from yaml file

jexp eval '.min(p=> p.total)' ./data/orders.yaml

Get 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.json

Result:

[{"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.json

Result:

["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 yaml

Result:

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 -b

Result:

[
  {
    "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.yaml

Result:

- 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 json

Result:

[
  {
    "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.json

Result:

[{"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.json

Result:

["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.json

Result:

7.58

Get the minimum of the article property from all the details:

jexp eval '.details.min(p=> p.article )' ./data/orders.json

Result:

"Apple"

Get the maximum "unitPrice * p.qty" from all the details:

jexp eval '.details.max(p=> p.unitPrice * p.qty )' ./data/orders.json

Result:

3.98

Get the middle value "unitPrice * p.qty" from all the details:

jexp eval 'round(.details.avg(p=> p.unitPrice * p.qty),2)' ./data/orders.json

Result:

2.35

Gets the sum of the total property:

jexp eval '.sum(p=> p.total )' ./data/orders.json

Result:

0

Get 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.json

Result:

7.91

Get the number of details where "unitPrice * p.qty " is less than 3:

jexp eval '.details.count(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json

Result:

8

Get 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.json

Result:

"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.json

Result:

"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 -b

Result:

{
  "article": "Banana",
  "unitPrice": 1.99,
  "qty": 1
}

Get the smallest article:

jexp eval '.details.min(p=> p.article )' ./data/orders.json

Result:

"Apple"

Get the total of all the details:

jexp eval '.details.max(p=> p.unitPrice * p.qty )' ./data/orders.json

Result:

3.98

Average 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.json

Result:

2

Get the total of the details of order 1:

jexp eval '.[1].details.sum(p=> p.unitPrice * p.qty )' ./data/orders.json

Result:

7.91

Gets the number of details where the subtotal is less than 3:

jexp eval '.details.count(p=> p.unitPrice * p.qty < 3 )' ./data/orders.json

Result:

8

Get 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.json

Result:

"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.json

Result:

"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.json

Result:

{"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.json

Result:

[{"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.json

Result:

[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.json

Result:

23.44

Get the list of items without repeating:

jexp eval '.details.distinct(p=>p.article)' ./data/orders.json -b

Result:

[
  "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 -b

Result:

[
  {
    "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.json

Result:

{"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 -b

Result:

{
  "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 -b

Result:

[
  {
    "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.json

Result:

["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.json

Result:

["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.json

Result:

["White grape"]

Articles of orders 20001 and 20003 that are not shared:

jexp eval '.[0].details.article.symmetricDifference(.[1].details.article)' ./data/orders.json

Result:

["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.44

js-expression

Js-expression is an extensible expression evaluator and parser.

Besides the operators, functions, variables, objects and arrays that are supported.

Documentation

Package Sidebar

Install

npm i jexp

Weekly Downloads

67

Version

1.0.4

License

Apache-2.0 License

Unpacked Size

48.3 kB

Total Files

31

Last publish

Collaborators

  • flaviolrita