Skip to content

Getting Started

Fx is a dual-purpose command-line tool tailored for JSON, providing both a terminal-based JSON viewer and a JSON processing utility. While the JSON viewer is crafted in Go and functions without external dependencies, the JSON processing tool is developed in JS, compatible with Node.js and Deno.

To use Fx:

sh
cat data.json | fx
cat data.json | fx

Or directly:

sh
fx data.json
fx data.json

JSON Processing

In fx, arguments are treated as JavaScript functions. Input data is passed sequentially through each provided function:

sh
echo '{"name": "world"}' | fx 'x => x.name' 'x => `Hello, ${x}!`'
echo '{"name": "world"}' | fx 'x => x.name' 'x => `Hello, ${x}!`'

You can also utilize standard JavaScript functions:

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

Fx also introduces syntactical sugar to enhance simplicity:

  • Reference input data using x or this.
  • Start expressions with . to avoid repeating the x => x.
sh
echo '{"name": "world"}' | fx .name '`Hello, ${x}!`'
echo '{"name": "world"}' | fx .name '`Hello, ${x}!`'

That's it. So simple!

INFO

Fx is short for f(x), capturing the essence of applying a function to input data.

Streaming Mode

Fx is designed to handle both line-delimited JSON and concatenated JSON streaming:

sh
echo '{"text": "Hello"}' '{"text": "World!"}' | fx .text
echo '{"text": "Hello"}' '{"text": "World!"}' | fx .text

Output:

Hello
World!
Hello
World!

For collective processing of a JSON stream as a single array, use the --slurp or -s flag:

sh
echo '{"text": "Hello"}' '{"text": "World!"}' | fx --slurp '.map(x => x.text)' '.join(", ")'
echo '{"text": "Hello"}' '{"text": "World!"}' | fx --slurp '.map(x => x.text)' '.join(", ")'

Output:

Hello, World!
Hello, World!

Interactive Mode

Fx provides an interactive mode that allows you to explore JSON interactively. To initiate the interactive mode, pipe a JSON into fx:

sh
cat data.json | fx
cat data.json | fx

Or:

sh
fx data.json
fx data.json

Easily navigate through your JSON using arrow or vim keys.

TIP

If you forget the available commands, press ? to display the key bindings.

Dig

Press . to adjust the current path. Fx utilizes a fuzzy search. Press tab or . to opt for the current suggestion.

Searching

Start a search with /, inputting a regex pattern. Navigate results with n (next) or N ( previous). By default, searches are case-insensitive. However, you can modify search sensitivity:

Case-sensitive: Add / at the pattern's end.

txt
/[a-z]/
/[a-z]/

Case-insensitive: Append /i flag.

txt
/[a-z]/i
/[a-z]/i

Printing

Fx uses stderr for rendering, allowing you to print values to stdout.

Press p to preview the current value. Press P to print the value to stdout.

You can use fx to select only specific part of the JSON and save it to a file:

sh
curl ... | fx > output.json
curl ... | fx > output.json

Text Selection

Text selection in fx differs due to mouse event redirection to stdin. Depending on your terminal, use specific key combinations to select text:

KeyTerminal
fnTerminal.app
optioniTerm2, Hyper
shiftLinux

TIP

To disable mouse event redirection, specify the FX_NO_MOUSE environment variable.