Skip to content
If you find fx useful, please consider supporting me:

Sponsor
A good coffee in Zurich costs around $7 ;-)

Advanced Usage

Pretty print JSON without starting the interactive mode:

sh
echo '{"name": "world"}' | fx .

Syntactic Sugar

Mapping is a frequently used process. To perform mapping, you can use the following command

sh
curl https://fx.wtf/example.json | fx '.issues.map(x => x.title)'

Or you can use the syntactic sugar:

sh
curl https://fx.wtf/example.json | fx '.issues[].title'

The [] operator is a shorthand for .flatMap(x => x).

It is useful when you need to flatten the array:

sh
curl https://fx.wtf/example.json | fx '.issues[].labels[].length'

TIP

Fx also provides even shorter syntax for mapping:

sh
curl https://fx.wtf/example.json | fx .issues @.title

The @ operator is a shorthand for .map(x => x). One benefit of using @ instead of .[] is that @ character does not require quotes in bash/zsh.

Edit-in-place

Use spread operator to modify the input:

sh
echo '{"count": 1}' | fx '{...x, count: x.count + 1}'

Or to modify nested objects, use comma , operator:

sh
fx example.json '.owner.login = "me", x'

INFO

The , operator is a JavaScript feature that evaluates each of its operands (from left to right) and returns the value of the last operand.

Use the save function to save the modified input:

sh
fx example.json 'x.name = x.name.toUpperCase(), x' save

Now the edited data will be saved to the same example.json file.

Big Integers

If a number is bigger than 253 – 1 (max safe integer in JS), fx will parse it as BigInt. To work with BigInts in JavaScript, use n suffix:

sh
echo '{"count": 1234567890987654321}' | fx '.count + 1n'

Infinity and NaN

fx will parse Infinity and NaN as is. Those values are not valid JSON, but they are valid values in JavaScript. Supported values are Infinity -Infinity INF, -INF NaN nan.

sh
echo '{"count": Infinity}' | fx '.count + 1'

Output: Infinity

TIP

Use --strict flag to throw an error on invalid JSON.

sh
echo '{"count": Infinity}' | fx --strict

Output:

Unexpected character 'I' on line 1.

  {"count": Infinity}
  ..........^

Non-JSON Data

For non-JSON content, use the --raw or -r flag:

sh
ls | fx -r '[x, x.includes(".md")]'

Combine --raw and --slurp (or -rs) to process as a single string array:

sh
ls | fx -rs '.filter(x => x.includes(".md"))'

Use the skip symbol to skip printing certain results:

sh
ls | fx -r '.includes(".md") ? this : skip'

Use the list function to print array as a list:

sh
ls | fx -rs '.filter(x => x.includes(".md"))' list

.fxrc.js

fx can recognize the .fxrc.js file, either in the current directory, home directory, or XDG config directory.

Here's how you can define custom functions:

js
function addOne(x) {
    return x + 1
}

Invoke your custom function in fx:

sh
echo '1' | fx addOne