2018-01-25 18:31:26 +00:00
|
|
|
|
<img src="https://user-images.githubusercontent.com/141232/35405308-4b41f446-0238-11e8-86c1-21f407cc8460.png" height="100" alt="fx">
|
|
|
|
|
|
|
|
|
|
# [![Build Status](https://travis-ci.org/antonmedv/fx.svg?branch=master)](https://travis-ci.org/antonmedv/fx)
|
2018-01-25 17:30:05 +00:00
|
|
|
|
|
|
|
|
|
Command-line JSON processing tool
|
|
|
|
|
|
|
|
|
|
## Features
|
|
|
|
|
|
|
|
|
|
* Don't need to learn new syntax
|
|
|
|
|
* Plain JavaScript
|
|
|
|
|
* Formatting and highlighting
|
2018-01-30 13:20:11 +00:00
|
|
|
|
* Standalone binary
|
2018-01-25 17:30:05 +00:00
|
|
|
|
|
|
|
|
|
## Install
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ npm install -g fx
|
|
|
|
|
```
|
|
|
|
|
|
2018-01-26 23:51:21 +00:00
|
|
|
|
Or download standalone binary from [releases](https://github.com/antonmedv/fx/releases) page.
|
|
|
|
|
|
2018-01-25 17:30:05 +00:00
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
|
|
Pipe into `fx` any JSON and anonymous function for reducing it.
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ fx [code ...]
|
|
|
|
|
```
|
|
|
|
|
|
2018-01-26 02:35:58 +00:00
|
|
|
|
Pretty print JSON without passing any arguments:
|
2018-01-25 17:30:05 +00:00
|
|
|
|
```
|
|
|
|
|
$ echo '{"key":"value"}' | fx
|
|
|
|
|
{
|
2018-06-26 14:59:04 +00:00
|
|
|
|
"key": "value"
|
2018-01-25 17:30:05 +00:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-01-26 02:35:58 +00:00
|
|
|
|
### Anonymous function
|
|
|
|
|
|
|
|
|
|
Use an anonymous function as reducer which gets JSON and processes it:
|
2018-01-25 17:30:05 +00:00
|
|
|
|
```
|
2018-01-26 02:35:58 +00:00
|
|
|
|
$ echo '{"foo": [{"bar": "value"}]}' | fx 'x => x.foo[0].bar'
|
2018-08-30 15:09:12 +00:00
|
|
|
|
value
|
2018-01-25 17:30:05 +00:00
|
|
|
|
```
|
|
|
|
|
|
2018-01-26 02:35:58 +00:00
|
|
|
|
### This Binding
|
|
|
|
|
|
2018-01-25 17:30:05 +00:00
|
|
|
|
If you don't pass anonymous function `param => ...`, code will be automatically transformed into anonymous function.
|
|
|
|
|
And you can get access to JSON by `this` keyword:
|
|
|
|
|
```
|
2018-01-26 02:35:58 +00:00
|
|
|
|
$ echo '{"foo": [{"bar": "value"}]}' | fx 'this.foo[0].bar'
|
2018-08-30 15:09:12 +00:00
|
|
|
|
value
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Dot
|
|
|
|
|
|
|
|
|
|
It is possible to omit `this` keyword:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ echo '{"foo": [{"bar": "value"}]}' | fx .foo[0].bar
|
|
|
|
|
value
|
2018-01-25 17:30:05 +00:00
|
|
|
|
```
|
|
|
|
|
|
2018-01-26 02:35:58 +00:00
|
|
|
|
### Chain
|
|
|
|
|
|
2018-01-25 17:30:05 +00:00
|
|
|
|
You can pass any number of anonymous functions for reducing JSON:
|
|
|
|
|
```
|
2018-01-26 02:35:58 +00:00
|
|
|
|
$ echo '{"foo": [{"bar": "value"}]}' | fx 'x => x.foo' 'this[0]' 'this.bar'
|
2018-08-30 15:09:12 +00:00
|
|
|
|
value
|
2018-01-25 17:30:05 +00:00
|
|
|
|
```
|
|
|
|
|
|
2018-01-26 02:35:58 +00:00
|
|
|
|
### Generator
|
|
|
|
|
|
2018-01-25 17:30:05 +00:00
|
|
|
|
If passed code contains `yield` keyword, [generator expression](https://github.com/sebmarkbage/ecmascript-generator-expression)
|
|
|
|
|
will be used:
|
|
|
|
|
```
|
2018-01-26 02:35:58 +00:00
|
|
|
|
$ curl ... | fx 'for (let user of this) if (user.login.startsWith("a")) yield user'
|
2018-01-25 17:30:05 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Access to JSON through `this` keyword:
|
|
|
|
|
```
|
2018-01-26 02:35:58 +00:00
|
|
|
|
$ echo '["a", "b"]' | fx 'yield* this'
|
2018-01-25 17:30:05 +00:00
|
|
|
|
[
|
2018-06-26 14:59:04 +00:00
|
|
|
|
"a",
|
|
|
|
|
"b"
|
2018-01-25 17:30:05 +00:00
|
|
|
|
]
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```
|
2018-01-26 02:35:58 +00:00
|
|
|
|
$ echo '["a", "b"]' | fx 'yield* this; yield "c";'
|
2018-01-25 17:30:05 +00:00
|
|
|
|
[
|
2018-06-26 14:59:04 +00:00
|
|
|
|
"a",
|
|
|
|
|
"b",
|
|
|
|
|
"c"
|
2018-01-25 17:30:05 +00:00
|
|
|
|
]
|
|
|
|
|
```
|
|
|
|
|
|
2018-01-26 02:35:58 +00:00
|
|
|
|
### Update
|
|
|
|
|
|
2018-01-25 17:30:05 +00:00
|
|
|
|
You can update existing JSON using spread operator:
|
|
|
|
|
|
|
|
|
|
```
|
2018-01-26 02:35:58 +00:00
|
|
|
|
$ echo '{"count": 0}' | fx '{...this, count: 1}'
|
2018-01-25 17:30:05 +00:00
|
|
|
|
{
|
2018-06-26 14:59:04 +00:00
|
|
|
|
"count": 1
|
2018-01-25 17:30:05 +00:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-01-26 02:35:58 +00:00
|
|
|
|
### Use npm package
|
|
|
|
|
|
|
|
|
|
Use any npm package by installing it globally:
|
|
|
|
|
```
|
|
|
|
|
$ npm install -g lodash
|
|
|
|
|
$ cat package.json | fx 'require("lodash").keys(this.dependencies)'
|
|
|
|
|
```
|
|
|
|
|
|
2018-01-26 16:06:25 +00:00
|
|
|
|
### Formatting
|
|
|
|
|
|
|
|
|
|
If you need something different then JSON (for example arguments for xargs) do not return anything from reducer.
|
2018-01-26 16:15:56 +00:00
|
|
|
|
`undefined` value printed into stderr by default.
|
2018-01-26 16:06:25 +00:00
|
|
|
|
```
|
|
|
|
|
echo '[]' | fx 'void 0'
|
|
|
|
|
undefined
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
echo '[1,2,3]' | fx 'this.forEach(x => console.log(x))' 2>/dev/null | xargs echo
|
|
|
|
|
1 2 3
|
|
|
|
|
```
|
|
|
|
|
|
2018-01-26 02:35:58 +00:00
|
|
|
|
### Other examples
|
|
|
|
|
|
2018-01-25 17:54:06 +00:00
|
|
|
|
Convert object to array:
|
|
|
|
|
```
|
2018-01-26 02:35:58 +00:00
|
|
|
|
$ cat package.json | fx 'Object.keys(this.dependencies)'
|
2018-01-25 17:54:06 +00:00
|
|
|
|
[
|
2018-09-14 17:00:36 +00:00
|
|
|
|
"@medv/prettyjson"
|
2018-01-25 17:54:06 +00:00
|
|
|
|
]
|
|
|
|
|
```
|
|
|
|
|
|
2018-03-19 17:15:05 +00:00
|
|
|
|
By the way, fx has shortcut for `Object.keys(this)`. Previous example can be rewritten as:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ cat package.json | fx this.dependencies ?
|
|
|
|
|
```
|
|
|
|
|
|
2018-01-25 17:30:05 +00:00
|
|
|
|
|
|
|
|
|
## Related
|
|
|
|
|
|
|
|
|
|
* [jq](https://github.com/stedolan/jq) – cli JSON processor on C
|
2018-01-26 15:38:13 +00:00
|
|
|
|
* [jl](https://github.com/chrisdone/jl) – functional sed for JSON on Haskell
|
2018-06-22 06:09:48 +00:00
|
|
|
|
* [xx](https://github.com/antonmedv/xx) – `fx`-like JSON tool on Go
|
2018-03-19 01:13:53 +00:00
|
|
|
|
* [ymlx](https://github.com/matthewadams/ymlx) - `fx`-like YAML cli processor
|
2018-01-25 17:30:05 +00:00
|
|
|
|
|
|
|
|
|
## License
|
|
|
|
|
|
|
|
|
|
MIT
|