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.
curl https://fx.wtf/example.json | fx len
uniq(x)
Returns unique values of an array.
curl https://fx.wtf/example.json | fx .tags uniq
sort(x)
Sorts an array.
curl https://fx.wtf/example.json | fx .tags sort
sortBy(fn)(x)
Sorts an array by the function fn
.
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 or an object using the function fn
.
curl https://fx.wtf/example.json | fx .issues 'map(x => x.title)'
map
is curried; it returns a function that can be called.
The second argument to fn
is the index or the key of the current item.
walk(fn)(x)
Walks an array or an object using the function fn
.
curl https://fx.wtf/example.json | fx 'walk(x => typeof x === "string" ? x.toUpperCase() : x)'
Same as map but works recursively. The second argument to fn
is the index or the key of the current item.
filter(fn)(x)
Filters an array or an object using the function fn
.
curl https://fx.wtf/example.json | fx .issues 'filter(x => x.state === "open")'
groupBy(fn)(x)
Groups an array by the function fn
.
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.
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.
echo '[[1,2],[3,4],[5]]' | fx flatten
reverse(x)
Reverses an array.
echo '[1,2,3,4,5]' | fx reverse
keys(x)
Returns an array of keys of an object.
echo '{"a": 1, "b": 2}' | fx keys
values(x)
Returns an array of values of an object.
echo '{"a": 1, "b": 2}' | fx values
list(x)
Prints an array as a list. This is useful for combining output with other commands.
echo '[1, 2, 3]' | fx list | xargs -I{} echo "The number is {}"
save(x)
Edits the input data in place.
fx example.json 'x.name = x.name.toUpperCase(), x' save
The edited data will be saved to the same example.json
file.
toBase64(x)
Encodes a string to base64.
curl https://fx.wtf/example.json | fx .text toBase64
fromBase64(x)
Decodes a base64 string.
echo '"SGVsbG8sIFdvcmxkIQ=="' | fx fromBase64
YAML.stringify(x)
Converts a JSON object to YAML.
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:
cat example.yaml | fx --yaml
Fx will also automatically detect YAML files with the .ya?ml
extension.
fx example.yaml