fx
Inspect JSON data in the terminal
SYNOPSIS
fx [options] [script] [files...]
cat input.json | fx [script]
PARAMETERS
-j, --json-path
Applies a JSON Path expression to filter or select specific elements from the input JSON. For example, fx -j '$.store.books'.
-m, --multiple
Processes multiple JSON inputs, treating each file or line from stdin as a separate JSON document. Useful when you have a stream of JSON objects.
-v, --version
Displays the current version of the fx command.
-h, --help
Shows the help message and available options for fx.
[script]
An optional JavaScript expression or file path. If an expression, it's applied to the input JSON (e.g., fx 'd => d.items.map(i => i.name)'). If a file, the script within the file is executed.
[files...]
Optional input JSON files. If no files are specified, fx reads JSON data from standard input (stdin).
DESCRIPTION
fx is a command-line tool for interactive JSON processing and manipulation. Unlike traditional text processing utilities, fx is designed specifically for working with JSON data, offering powerful features for pretty-printing, filtering, and transforming JSON payloads.
Its key strength lies in its interactive REPL (Read-Eval-Print Loop) mode, which allows users to explore JSON data in real-time by typing JavaScript expressions. It also supports non-interactive usage, where you can pipe JSON data to fx and apply a JavaScript script or a JSON Path expression directly, making it highly flexible for scripting and automation.
Built on Node.js, fx leverages the familiarity and power of JavaScript, enabling complex data transformations that might be more cumbersome with other tools. It's particularly useful for developers and system administrators who frequently deal with API responses, configuration files, or logs in JSON format.
CAVEATS
Since fx is built on Node.js, it requires Node.js to be installed on the system. This can make it heavier in terms of dependencies and memory footprint compared to native binaries like jq, especially for very large JSON files. Performance might vary depending on the complexity of the JavaScript script and the size of the input data.
INTERACTIVE MODE
When fx is invoked without a script argument and receives JSON from stdin or a file, it enters an interactive REPL mode. In this mode, the input JSON is assigned to the variable d. Users can then type JavaScript expressions directly (e.g., d.data[0].name) to explore, filter, or transform the data, seeing immediate results. This provides a highly dynamic and exploratory way to work with JSON.
JAVASCRIPT SCRIPTING
One of fx's most powerful features is its ability to use arbitrary JavaScript for transformations. The input JSON is accessible as the variable d within the script. This allows for complex operations, custom filtering, data aggregation, and integration with standard JavaScript functions, offering a level of flexibility not commonly found in simpler JSON processing tools.
HISTORY
fx is a relatively modern tool that emerged with the widespread adoption of JSON as a primary data interchange format and the popularity of Node.js. Developed by Andrej Bauer, it gained traction as a user-friendly alternative to more complex JSON processing tools, primarily due to its intuitive interactive mode and the power of JavaScript for data manipulation.