Skip to content

Getting Started

Fx is a CLI for JSON: it shows JSON interactively in your terminal, and lets you transform JSON with JavaScript. Fx is written in Go and uses goja as its embedded JavaScript engine.

Run this command to start fx in interactive mode:

sh
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}!`'

You can also utilize standard JavaScript functions:

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

Fx also introduces syntactic 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}!`'

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

Output:

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(", ")'

Output:

Hello, World!

Interactive Mode

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

sh
curl -i https://fx.wtf/example.json | fx

TIP

Yes, fx can show curl headers (with the -i flag) in the interactive mode.

Or:

sh
fx example.json

TIP

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

Easily navigate through your JSON using arrow keys or Vim keys.

Press . to adjust the current path. Press tab or . to accept the current suggestion.

Press @ to start a fuzzy search of JSON paths and jump to the first match.

Press [ or ] to jump to the previous location or to the next location in history.

Searching

Press /, enter a regex pattern, press enter. Navigate to the next search result with n and to the previous search result with N.

By default, the search is case-insensitive. However, you can modify search sensitivity:

Case-Sensitive: Add / at the end of the pattern.

txt
/[a-z]/

Case-Insensitive: Append the /i flag.

txt
/[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 a specific part of the JSON and save it to a file:

sh
curl ... | fx > output.json

This command will run fx in the interactive mode, so you can navigate JSON, find the necessary part, and press P to print it to output.json.

To simply pretty-print the whole JSON to output.json, use:

sh
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.