fp-json-cli v0.1.2
FP Json CLI
fq is a command line tool that embodies the Unix philosophy of small, composable functions that can be piped together using the | operator. It simplifies the process of filtering and querying JSON data, drawing inspiration from popular tools like jq et al., while aiming to provide a more familiar experience for JavaScript and Typescript developers.
Table of Contents
Installation
npm install --global fp-json-cliyarn global add fp-json-clipnpm install --global fp-json-cliUsage
You can either read JSON data from stdin by using a command like cat package.json | fq ".name", or provide the file name as the second argument, eg. fq ".name" package.json.
One of the more interesting use cases for fq is when you want to change the format of JSON data. By piping together multiple fq operators, you can transform JSON data from one format to another easily.
Examples
List all Operators in Reverse Order
fq list --json | fq "chain(.alias) | sort(.) | reverse(.)"fq list --json: Write all available operators in JSON format to stdout.chain(.alias): Is a shorthand forflatMap(get(alias)), which concatenates the alias JSON arrays together.sort(.): Sort the JSON array.reverse(.): Reverse the sorted array.
Remove Properties from API Data
curl -X GET https://jsonplaceholder.typicode.com/users > users.json # save response to a file
fq -o users-out.json "map(u(omit(company), p(address, .address|omit(geo)))) | filter(.id >= 4) | take(2)" users.jsonmap(u(omit(company), p(address, .address|omit(geo)))): This operation maps each user object in the JSON data. It omits thecompanyfield from the user object and re-projects theaddressfield without thegeofield.filter(.id >= 4): Filters the mapped user objects based on the condition that theidfield is greater than or equal to 4.take(2): Takes the first 2 filtered user objects.
The final result of these operations is written to the users-out.json file.
CLI
fq/0.1.0
Usage:
$ fq <query> [file]
Commands:
<query> [file] Run fp style operators on input file or stdin
list List available operators
For more info, run any command with the `--help` flag:
$ fq --help
$ fq list --help
Options:
-o, --out <path> Write the result to a file
--no-nl Control new line at the end output (default: true)
--show-ast Dump AST to stdout
--show-ir Dump intermediate representation to stdout
-h, --help Display this message
-v, --version Display version number
Examples:
fq "map(union(pick(email, name), project(age, meta.age)) | filter(.age > 2)" users.jsonOperators
| Name | Alias |
|---|---|
| add | |
| constant | c |
| count | len |
| divide | div |
| entries | |
| equals | eq |
| every | and |
| filter | |
| flatMap | chain |
| flow | |
| get | pluck |
| greaterThan | gt |
| greaterThanEquals | gte |
| identity | id, i |
| includes | has |
| lessThan | lt |
| lessThanEquals | lte |
| map | |
| multiply | mul |
| not | |
| notEquals | neq |
| omit | |
| pick | |
| project | p |
| range | |
| reverse | |
| skip | |
| some | or |
| sort | |
| subract | sub |
| take | |
| union | u |
| unique | uniq |
TODO
- Error reporting
- Type checking
- Reduce/fold operator
- Streaming parsing and serializing for large files
- Highlight output similar to
jq