Discover how to use fx effectively, a JSON manipulation command line tool
fx is a popular JSON manipulation command line tool written in Go. It can be installed via brew or downloaded as a standalone binary.
brew install fx
fx comes in handy with a curl command. If you have some API that returns JSON and you want to dig into the structure or just see it, pipe JSON into fx.
curl https://fx.wtf/example.json | fx
fx has a really neat interactive mode for digging into JSON.
You can use your mouse 🐁 or arrow keys to navigate JSON. fx supports folds, click on a field to expand it, or press the right arrow to expand. Press e
key to expand all fields recursively. Press Shift + e
to collapse everything back. Or use key bindings similar to vim.
Pretty printing
Sometimes you don’t need the interactive digger and want to pretty print JSON to stdout. This is can be done by adding .
argument to fx command.
curl https://fx.wtf/example.json | fx .
Or pass a file as an argument:
fx data.json .name
Each argument to fx can be some JavaScript function, let’s create .fxrc.js
file where we put useful functions and snippets for reuse.
.fxrc.js
For example, I have one API which requires documents to be base64 encoded. Let’s see how it’s can be done.
Create some function and assign in to global. For example, a base64 snippet.
global.base64 = str => Buffer.from(str).toString('base64')
Now I’m able to do something like this:
fx data.json '{value: base64(JSON.stringify(this))}' | curl -X POST
Or if split into separate functions:
fx data.json JSON.stringify base64 '{value: this}' | curl -X POST
Edit in-place
With fx you can easily modify JSON objects by using ...
spread operator.
echo '{"count": 0}' | fx '{...this, count: this.count+1}'
But if you try to modify a file and save it on a disk in one command, you corrupt your file.
fx data.json '{...this, count: this.count+1}' > new-data.json
Searching JSON
fx supports interactive JSON searching. Press /
can type your pattern to search.
To jump to the next pattern match press n
.
Learn by examples
Update JSON using the spread operator.
$ echo '{"count": 0}' | fx '{...this, count: 1}'
{
"count": 1
}
Extract values from maps.
fx commits.json | fx .[].author.name
The last example actually the same as this.map(x => x.author.name)
but this syntax also can be used on objects and be nested.
fx .comments[].authors[].names
The next examples all do the same thing.
fx 'x => x.foo[0].bar'
fx 'this.foo[0].bar'
fx '.foo[0].bar'
fx '.foo[0]' | fx '.bar'
fx '.foo[0]' '.bar'
If you need to run interactive mode only on part of JSON, pipe it again to fx.
cat data.json | fx .value[0].nested | fx
Streaming
fx supports JSON streaming as well. You can use it for parsing logs, etc.
kubectl logs ... | fx .message
Or just concatenate a few JSON files with cat
and pipe to fx.
cat *.json | fx .length
🎨 Themes support
fx supports themes as well. You can change colors and indent.
export FX_THEME=1
To test all themes type next command:
fx --themes
I hope you enjoy using fx!