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 lenuniq(x)
Returns unique values of an array.
curl https://fx.wtf/example.json | fx .tags uniqsort(x)
Sorts an array.
curl https://fx.wtf/example.json | fx .tags sortsortBy(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 using the function fn. The second argument to fn is the index of the current item.
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:
echo '{"a": 1}{"a": 2}' | fx 'map(x => x.a * 10)'10
20To map over the values of an object, use Object.entries:
echo '{"a": [1, 2], "b": [3]}' | fx 'Object.entries(x).map(([k, v]) => [k, v.length])' Object.fromEntries{
"a": 2,
"b": 1
}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)'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.
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 flattenreverse(x)
Reverses an array.
echo '[1,2,3,4,5]' | fx reversekeys(x)
Returns an array of keys of an object.
echo '{"a": 1, "b": 2}' | fx keysvalues(x)
Returns an array of values of an object.
echo '{"a": 1, "b": 2}' | fx valueslist(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' saveThe 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.
curl https://fx.wtf/example.json | fx .text toBase64fromBase64(x)
Decodes a base64 string.
echo '"SGVsbG8sIFdvcmxkIQ=="' | fx fromBase64YAML.stringify(x)
Converts a JSON object to YAML.
curl https://fx.wtf/example.json | fx '.users[]' YAML.stringifyYAML.parse(x)
Converts a YAML string to JSON.
INFO
You can use the --yaml flag to parse YAML:
cat example.yaml | fx --yamlFx will also automatically detect YAML files with the .ya?ml extension.
fx example.yaml