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 fxfx 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 | fxPress enter or click to view image in full size
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 .nameEach 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 POSTOr if split into separate functions:
fx data.json JSON.stringify base64 '{value: this}' | curl -X POSTEdit 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 +=1, this' saveSearching JSON
Press enter or click to view image in full size
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.nameThe 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[].namesThe 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 | fxStreaming
fx supports JSON streaming as well. You can use it for parsing logs, etc.
kubectl logs ... | fx .messageOr 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=1Press enter or click to view image in full size
To test all themes type next command:
fx --themesI hope you enjoy using fx!