Skip to content

Built-in Functions ​

Fx provides a set of built-in functions that can be used to process JSON data.

len(x) ​

Returns the length of an array, string, or object.

sh
curl https://fx.wtf/example.json | fx len

uniq(x) ​

Returns unique values of an array.

sh
curl https://fx.wtf/example.json | fx .tags uniq

sort(x) ​

Sorts an array.

sh
curl https://fx.wtf/example.json | fx .tags sort

sortBy(fn)(x) ​

Sorts an array by the function fn.

sh
echo '[1,2,3,4,5]' | fx 'sortBy(x => x % 2 > 0)'

sortBy is curried; it returns a function that can be called.

map(fn)(x) ​

Maps an array using the function fn. The second argument to fn is the index of the current item.

sh
curl https://fx.wtf/example.json | fx .issues 'map(x => x.title)'

map is curried; it returns a function that can be called.

If x is not an array, fn is applied to x itself. This makes map work on JSON streams, where each value is processed separately:

sh
echo '{"a": 1}{"a": 2}' | fx 'map(x => x.a * 10)'
output
10
20

To map over the values of an object, use Object.entries:

sh
echo '{"a": [1, 2], "b": [3]}' | fx 'Object.entries(x).map(([k, v]) => [k, v.length])' Object.fromEntries
output
{
  "a": 2,
  "b": 1
}

walk(fn)(x) ​

Walks an array or an object using the function fn.

sh
curl https://fx.wtf/example.json | fx 'walk(x => typeof x === "string" ? x.toUpperCase() : x)'

Recursively visits every array item and object value, bottom-up, replacing each with the result of fn. The second argument to fn is the index or the key of the current item.

filter(fn)(x) ​

Filters an array using the function fn. If x is not an array, returns x when fn(x) is truthy and skips it otherwise, which makes filter work on JSON streams.

sh
curl https://fx.wtf/example.json | fx .issues 'filter(x => x.state === "open")'

groupBy(fn)(x) ​

Groups an array by the function fn.

sh
curl https://fx.wtf/example.json | fx .issues 'groupBy(x => x.state)'

groupBy is curried; it returns a function that can be used to group an array.

chunk(size)(x) ​

Chunks an array into smaller arrays of a given size.

sh
echo '[1,2,3,4,5]' | fx 'chunk(2)'

chunk is curried; it returns a function that can be used to chunk an array.

flatten(x) ​

Flattens an array.

sh
echo '[[1,2],[3,4],[5]]' | fx flatten

reverse(x) ​

Reverses an array.

sh
echo '[1,2,3,4,5]' | fx reverse

keys(x) ​

Returns an array of keys of an object.

sh
echo '{"a": 1, "b": 2}' | fx keys

values(x) ​

Returns an array of values of an object.

sh
echo '{"a": 1, "b": 2}' | fx values

list(x) ​

Prints an array as a list. This is useful for combining output with other commands.

sh
echo '[1, 2, 3]' | fx list | xargs -I{} echo "The number is {}"

save(x) ​

Edits the input data in place.

sh
fx example.json 'x.name = x.name.toUpperCase(), x' save

The edited data will be saved to the same example.json file.

The file must be passed as the first argument and contain a single JSON value. save writes to a temporary file and renames it over the original, keeping the file's permissions. It refuses to write through a symbolic link.

toBase64(x) ​

Encodes a string to base64.

sh
curl https://fx.wtf/example.json | fx .text toBase64

fromBase64(x) ​

Decodes a base64 string.

sh
echo '"SGVsbG8sIFdvcmxkIQ=="' | fx fromBase64

YAML.stringify(x) ​

Converts a JSON object to YAML.

sh
curl https://fx.wtf/example.json | fx '.users[]' YAML.stringify

YAML.parse(x) ​

Converts a YAML string to JSON.

INFO

You can use the --yaml flag to parse YAML:

sh
cat example.yaml | fx --yaml

Fx will also automatically detect YAML files with the .ya?ml extension.

sh
fx example.yaml