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/issues.json | fx len
curl https://fx.wtf/issues.json | fx len

uniq(x)

Returns unique values of an array.

sh
curl https://fx.wtf/issues.json | fx '.[].labels[].name' uniq
curl https://fx.wtf/issues.json | fx '.[].labels[].name' uniq

sort(x)

Sorts an array.

sh
curl https://fx.wtf/issues.json | fx '.[].labels[].name' sort
curl https://fx.wtf/issues.json | fx '.[].labels[].name' sort

sortBy(fn)

Sorts an array by a fn function.

sh
echo '[1,2,3,4,5]' | fx 'sortBy(Math.sin)'
echo '[1,2,3,4,5]' | fx 'sortBy(Math.sin)'

map(fn)

Maps an array with a fn function.

sh
curl https://fx.wtf/commits.json | fx 'map(x => x.commit.message)'
curl https://fx.wtf/commits.json | fx 'map(x => x.commit.message)'

groupBy(fn)

Groups an array by a fn function.

sh
curl https://fx.wtf/commits.json | fx 'groupBy(x => x.author?.login)'
curl https://fx.wtf/commits.json | fx 'groupBy(x => x.author?.login)'

chunk(size)

Chunks an array into smaller arrays of a given size.

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

flatten(x)

Flattens an array.

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

reverse(x)

Reverses an array.

sh
echo '[1,2,3,4,5]' | fx reverse
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
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
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 {}"
echo '[1, 2, 3]' | fx list | xargs -I{} echo "The number is {}"