jq to fx: Command Equivalents Cheat Sheet
jq has its own filter language; fx takes plain JavaScript expressions instead. Here is how common jq commands map to fx, with real output. Install fx to follow along.
Examples use users.json:
[
{"id": 1, "name": "Alice", "role": "admin", "age": 34, "email": "alice@example.com"},
{"id": 2, "name": "Bob", "role": "dev", "age": 27, "email": null},
{"id": 3, "name": "Carol", "role": "dev", "age": 41, "email": "carol@example.com"}
]and user.json:
{"id": 1, "name": "Alice", "address": {"city": "Berlin", "zip": "10115"}, "password": "hunter2"}jq vs fx Quick Reference
| jq | fx |
|---|---|
jq . | fx . |
jq -r '.name' | fx .name (strings print raw by default) |
jq '.address.city' | fx .address.city |
jq '.[0]' | fx '.[0]' |
jq '.[] | .name' | fx '@.name' list |
jq 'map(.name)' | fx '@.name' |
jq 'map(select(.age > 30))' | fx '?.age > 30' |
jq keys_unsorted | fx keys |
jq length | fx len |
jq to_entries | fx Object.entries |
jq from_entries | fx Object.fromEntries |
jq 'group_by(.role)' | fx 'groupBy(x => x.role)' (returns an object) |
jq 'sort_by(.age)' | fx 'sortBy(x => x.age)' |
jq unique | fx uniq |
jq add (numbers) | fx '.reduce((a, b) => a + b, 0)' |
jq -R | fx -r |
jq -s | fx -s |
jq -c | fx JSON.stringify |
jq 'del(.password)' | fx 'delete x.password, x' |
jq '.address.city = "Munich"' | fx '.address.city = "Munich", x' |
jq '.email // "n/a"' | fx '.email ?? "n/a"' |
jq '"\(.name) is \(.age)"' | fx '`${x.name} is ${x.age}`' |
Select a Field or Array Element
Paths look the same as in jq. A string result prints without quotes, so jq -r is not needed.
fx user.json '.address.city'BerlinWARNING
jq returns null for a missing key; fx throws TypeError: Cannot read property 'name' of undefined. Use JavaScript optional chaining: fx user.json '.company?.name'.
Map and Filter Arrays Like jq select
@ maps, ? filters. Each argument receives the previous result, so jq's | becomes a new argument. This is jq's map(select(.age > 30)) | map(.name):
fx users.json '?.age > 30' '@.name'[
"Alice",
"Carol"
]jq's .[] emits every element as a separate value. In fx, .[] flattens an array but still returns one array, so .[].name gives the same result as @.name. To print one element per line, add list:
fx users.json '@.name' listAlice
Bob
Caroljq keys, length, to_entries, and from_entries in fx
fx keys keeps the original key order, like jq's keys_unsorted; jq's keys sorts them. len works on arrays, objects, and strings: fx users.json len prints 3, fx user.json .name len prints 5.
jq's to_entries and from_entries map to Object.entries and Object.fromEntries:
fx user.json .address Object.entries[
[
"city",
"Berlin"
],
[
"zip",
"10115"
]
]group_by, sort_by, unique, and add
sortBy and uniq work as in jq. fx users.json 'sortBy(x => x.age)' '@.name' gives ["Bob", "Alice", "Carol"]; negate the key (sortBy(x => -x.age)) for descending order.
groupBy returns an object keyed by group, where jq's group_by returns an array of arrays. Add values to get jq's shape. @@.name maps twice, like jq's map(map(.name)):
fx users.json 'groupBy(x => x.role)' values '@@.name'[
[
"Alice"
],
[
"Bob",
"Carol"
]
]fx has no add. For numbers use reduce: fx users.json '@.age' '.reduce((a, b) => a + b, 0)' prints 102. For strings use join: fx users.json '@.name' '.join(", ")' prints Alice, Bob, Carol.
Raw Input, Slurp, and Compact Output (-r, -s, -c)
The flags are named differently. fx -r reads each input line as a string, like jq -R. There is no output -r because strings already print raw.
cat access.txt | fx -r '.split(" ")[1]'/health
/login
/users-s collects a stream of JSON values (like NDJSON logs) into one array, as in jq: cat app.log | fx -s '?.level === "error"' keeps only the error lines.
fx has no -c flag. Use JSON.stringify for compact output. For jq's -c '.[]' (one object per line), stringify each element and print as a list:
fx users.json '@JSON.stringify' list{"id":1,"name":"Alice","role":"admin","age":34,"email":"alice@example.com"}
{"id":2,"name":"Bob","role":"dev","age":27,"email":null}
{"id":3,"name":"Carol","role":"dev","age":41,"email":"carol@example.com"}Update, Delete, and Default Values
jq's del(.password) becomes a JavaScript delete. The comma operator returns x once the delete is done:
fx user.json 'delete x.password, x'{
"id": 1,
"name": "Alice",
"address": {
"city": "Berlin",
"zip": "10115"
}
}Assignment works the same way: fx user.json '.address.city = "Munich", x', or spread: fx user.json '{...x, id: 42}'. To write the change back to the file, add save (edit in place).
jq's alternative operator // becomes ??:
fx users.json '@.email ?? "n/a"' gives ["alice@example.com", "n/a", "carol@example.com"]. ?? replaces only null and undefined. jq's // also replaces false.
String Interpolation and @csv
JavaScript template literals replace jq's "\(.name)":
fx users.json '@`${x.name} is ${x.age}`' listAlice is 34
Bob is 27
Carol is 41jq's @csv quotes strings and doubles any " inside them. There is no built-in for it in fx, but a short map does the same, and matches jq -r '.[] | [.id, .name, .role] | @csv':
fx users.json '@[x.id, x.name, x.role].map(v => typeof v === "string" ? `"${v.replace(/"/g, `""`)}"` : v).join(",")' list1,"Alice","admin"
2,"Bob","dev"
3,"Carol","dev"For repeated use, define the helper once in .fxrc.js.