Welcome to rjq
rjq is a simple and lightweight CLI JSON filtering tool designed for Windows and Linux.
Flags
--load- Loads JSON data from file.--query- Query to be performed on the dataset.--params- Selects specific fields that will be returned as result.
Commands
rjq --load="<file name>"- Loads JSON data from file and returns all the data as result.rjq --query="<query string>"- Sets query string.rjq --params="<comma separated field names>"- Sets fileds for the result
Usage
rjq --load="test.json" --query="<query string>" --params="<comma separated field names>"- Loads data from file, applies the query, returns array of objects with fields specified by '--params' flag.-
stto --json cpython | rjq --query="<query string>" --params="<comma separated field names>"- Piping output from other program.N.B. stto is a line of code counter used as an example here.
Query structure
Query is set by --query flag, e.g. --query="age > 30 && percentage >= 50.0".
rjq has a simple query structure, it follows [field_name] [operator] [Value] pattern.
-
field_name- It is the field name from the supplied dataset. For nested objects '.' operator should be used for accessing values, e.g. to access 'town' value from the following json:{ "_id": "88L33FM4VQBB1QYH", "address": { "postode": "CR45 9NE", "street": "3137 Stich Avenue", "town": "Swanley" } }use
address.townin the field_name of the query.N.B.
field_nameshould not be wrapped in any quotes. -
Operator- The following operators are supported for now:=-equal_tooperator, checks if both sides are same.!=-not_equalsoperator, checks if both sides are NOT same.>-greather_thanoperator, checks ifleftis greater than toright.<-less_thanoperator, checks ifleftis lesser than toright.>=-greather_than_or_equaloperator, checks ifleftis greater than torightor same.<=-less_than_or_equaloperator, checks ifleftis lesser than torightor same.&&-andoperator, joins two Binary expressions, e.g.age > 18 && percentage < 34.0.||-oroperator, joins two Binary expressions, e.g.age > 18 || percentage < 34.0.
Value- rjq supports 3 types of values,string,boolean&number.String- strings should be wrapped in single quotes (') only, e.g.'name','address'etc.Boolean-true/falsewithout any quotes.Number- Any number without quotes. For comparing decimal numbers decimal point should be provided, e.g. for queringage&percentageof the following -{ "age": 30, ... "percentage": 56.34, ... }use something likeage > 18 && percentage > 50.0.
Parameters
Output parameters are set by --params flag e.g. --params="name, address.town".
Dot walking is supported for nested object fields.
Project layout
LICENSE # License file
README.md # Gitgub ReadMe file
Cargo.toml # Project Config file
src/
├── main.rs # Entry point for the program.
├── query
│ ├── interpreter.rs # Interprets the AST and returns boolean value after evaluating the query.
│ ├── lexer.rs # Lexer of the program, tokenizes the query string.
│ └── parser.rs # Parser of the program, creates Abstract syntax tree from the tokens generated by the lexer.
├── query.rs # Wrapper file
└── utils.rs # Utility file.