
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
The fx npm package is a command-line JSON processing tool that allows users to manipulate and transform JSON data using JavaScript expressions. It is designed to be simple and powerful, enabling quick and efficient JSON data processing directly from the terminal.
Basic JSON Filtering
This feature allows you to filter JSON data to extract specific fields. In this example, the command extracts the 'name' field from the JSON object.
echo '{"name": "John", "age": 30}' | fx '.name'
Complex JSON Transformations
This feature enables complex transformations of JSON data using JavaScript functions. The example command maps over an array of objects and extracts the 'name' field from each object.
echo '[{"name": "John", "age": 30}, {"name": "Jane", "age": 25}]' | fx 'map(x => x.name)'
Chaining Operations
This feature allows chaining multiple operations to perform more complex data manipulations. The example command filters the array to include only objects where 'age' is greater than 25 and then maps to extract the 'name' field.
echo '[{"name": "John", "age": 30}, {"name": "Jane", "age": 25}]' | fx 'filter(x => x.age > 25).map(x => x.name)'
jq is a lightweight and flexible command-line JSON processor. It allows for powerful data manipulation and transformation using its own domain-specific language. Compared to fx, jq is more feature-rich and has a steeper learning curve due to its unique syntax.
The json command-line tool is a simpler alternative for working with JSON data. It allows for basic filtering and transformation using JavaScript expressions. While it is less powerful than fx, it is easier to use for straightforward tasks.
jshon is another command-line tool for JSON manipulation. It provides a set of commands for extracting and transforming JSON data. Compared to fx, jshon is more Unix-like and script-friendly but may require more verbose commands for complex operations.
A non-interactive, JavaScript version of the fx. Short for Function eXecution or f(x).
npm i -g fx
Or use npx:
cat file.json | npx fx .field
Or use deno:
cat file.json | deno run -A npm:fx .field
Fx treats arguments as JavaScript functions. Fx passes the input data to the first function and then passes the result of the first function to the second function and so on.
echo '{"name": "world"}' | fx 'x => x.name' 'x => `Hello, ${x}!`'
Use this
to access the input data. Use .
at the start of the expression to
access the input data without a x => x
part.
echo '{"name": "world"}' | fx '.name' '`Hello, ${this}!`'
Use other JS functions to process the data.
echo '{"name": "world"}' | fx 'Object.keys'
Fx can process a stream of json objects. Fx will apply arguments to each object.
echo '{"name": "hello"}\n{"name": "world"}' | fx '.name'
If you want to process a stream of json objects as a single array, use the --slurp or -s flag.
echo '{"name": "hello"}\n{"name": "world"}' | fx --slurp '.map(x => x.name)' '.join(", ")'
If you want to process non-JSON data, use the --raw or -r flag.
ls | fx -r '[this, this.includes(".md")]'
You can use --raw and --slurp (or -rs) together to get a single array of strings.
ls | fx -rs '.filter(x => x.includes(".md"))'
Fx has a special symbol skip for skipping the printing of the result.
ls | fx -r '.includes(".md") ? this : skip'
Fx comes with a set of useful functions: uniq, sort, groupBy, chunk, zip.
cat file.json | fx 'uniq' 'sort' 'groupBy(x => x.name)'
You can use special function save to edit-in-place the input data.
fx file.json 'x.name = x.name.toUpperCase(), x' 'save'
The edited data will be saved to the same file.json
file.
Fx has a shortcut for the map function. Fox example, this.map(x => x.commit.message)
can be rewritten without leading dot and without x => x
parts.
curl https://api.github.com/repos/antonmedv/fx/commits | fx 'map(.commit.message)'
echo '[{"name": "world"}]' | fx 'map(`Hello, ${x.name}!`)'
Fx has a special syntax for the flatMap function. Fox example,
.issues.flatMap(x => x.labels.flatMap(x => x))
can be rewritten in the next way.
curl https://fx.wtf/example.json | fx '.issues[].labels[]'
Fx supports .fxrc.js
file in the current directory, or in the home directory, or in XDG config directory.
Put the next code in the .fxrc.js
file to make myFunction
available in the fx.
function addOne(x) {
return x + 1
}
Now you can use addOne
in the fx.
echo '1' | fx addOne
If you would like to create global variables use var
instead of let
or const
.
FAQs
Command-line JSON viewer
The npm package fx receives a total of 187,616 weekly downloads. As such, fx popularity was classified as popular.
We found that fx demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.