You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
Go to file
Anton Medvedev 17b30f2622 Release 1.1.1 6 years ago
.gitignore Add standalone binary dist 6 years ago
.travis.yml lower node.js version requirements (#6) 6 years ago
LICENSE Create LICENSE 6 years ago
README.md Use @medv/prettyjson 6 years ago
index.js Use @medv/prettyjson 6 years ago
package.json Release 1.1.1 6 years ago
test.js Use ava for tests 6 years ago

README.md

fx

Build Status

Command-line JSON processing tool

Features

  • Don't need to learn new syntax
  • Plain JavaScript
  • Formatting and highlighting
  • Standalone binary

Install

$ npm install -g fx

Or download standalone binary from releases page.

Usage

Pipe into fx any JSON and anonymous function for reducing it.

$ fx [code ...]

Pretty print JSON without passing any arguments:

$ echo '{"key":"value"}' | fx
{
  "key": "value"
}

Anonymous function

Use an anonymous function as reducer which gets JSON and processes it:

$ echo '{"foo": [{"bar": "value"}]}' | fx 'x => x.foo[0].bar'
"value"

This Binding

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:

$ echo '{"foo": [{"bar": "value"}]}' | fx 'this.foo[0].bar'
"value"

Chain

You can pass any number of anonymous functions for reducing JSON:

$ echo '{"foo": [{"bar": "value"}]}' | fx 'x => x.foo' 'this[0]' 'this.bar'
"value"

Generator

If passed code contains yield keyword, generator expression will be used:

$ curl ... | fx 'for (let user of this) if (user.login.startsWith("a")) yield user'

Access to JSON through this keyword:

$ echo '["a", "b"]' | fx 'yield* this'
[
  "a",
  "b"
]
$ echo '["a", "b"]' | fx 'yield* this; yield "c";'
[
  "a",
  "b",
  "c"
]

Update

You can update existing JSON using spread operator:

$ echo '{"count": 0}' | fx '{...this, count: 1}'
{
  "count": 1
}

Use npm package

Use any npm package by installing it globally:

$ npm install -g lodash
$ cat package.json | fx 'require("lodash").keys(this.dependencies)'

Formatting

If you need something different then JSON (for example arguments for xargs) do not return anything from reducer. undefined value printed into stderr by default.

echo '[]' | fx 'void 0'
undefined
echo '[1,2,3]' | fx 'this.forEach(x => console.log(x))' 2>/dev/null | xargs echo
1 2 3

Other examples

Convert object to array:

$ cat package.json | fx 'Object.keys(this.dependencies)'
[
  "@medv/prettyjson",
  "get-stdin",
  "meow"
]

By the way, fx has shortcut for Object.keys(this). Previous example can be rewritten as:

$ cat package.json | fx this.dependencies ?
  • jq cli JSON processor on C
  • jl functional sed for JSON on Haskell
  • xx fx-like JSON tool on Go
  • ymlx - fx-like YAML cli processor

License

MIT