Listing All Paths and Keys in a JSON Document
Print every key and path in a JSON file, find where a value lives, or summarize an unfamiliar API response as a schema. All examples use fx (install fx) and this input, order.json:
{
"id": "ord_1042",
"status": "shipped",
"customer": {"name": "Alice Chen", "email": "alice@example.com"},
"items": [
{"sku": "KB-01", "qty": 1, "price": 79.9},
{"sku": "MS-07", "qty": 2, "price": 24.5}
],
"shipping": {"address": {"city": "Berlin", "zip": "10115"}, "tracking": null},
"paid": true
}List Top-Level Keys in JSON
The keys built-in returns the keys of an object:
fx order.json keysOutput:
[
"id",
"status",
"customer",
"items",
"shipping",
"paid"
]Add len to count them: fx order.json keys len prints 6.
Navigate first for a nested object (fx order.json '.customer' keys). For an array of records, map keys over every element, then flatten and deduplicate:
fx order.json '.items' '@keys' flatten uniqHere both items share sku, qty, price, so that is the result; on mixed records you get every field any record has.
List Every JSON Path Recursively
A short recursive function collects paths in .a.b[0].c form. list prints one per line:
fx order.json 'x => {
const paths = [];
const visit = (v, p) => {
if (p) paths.push(p);
if (v && typeof v === "object")
for (const [k, c] of Object.entries(v))
visit(c, Array.isArray(v) ? `${p}[${k}]` : `${p}.${k}`);
};
visit(x, "");
return paths;
}' listOutput (abbreviated):
.id
.status
.customer
.customer.name
.customer.email
.items
.items[0]
...
.shipping.address
.shipping.address.city
.shipping.address.zip
.shipping.tracking
.paidEvery printed path is valid fx syntax, so it can be passed straight back: fx order.json '.shipping.address.city' prints Berlin. Keys that are not valid identifiers, such as content-type, need bracket notation: '.headers["content-type"]'.
Print Leaf Paths With Their Values
Leaves are the non-object values. Save a leaf walker as a reusable function in .fxrc.js (current directory, home directory, or XDG config directory). It returns {path, value} pairs:
function paths(x) {
const out = []
const visit = (v, p) => {
if (v && typeof v === 'object') {
for (const [k, c] of Object.entries(v))
visit(c, Array.isArray(v) ? `${p}[${k}]` : `${p}.${k}`)
} else {
out.push({path: p, value: v})
}
}
visit(x, '')
return out
}Format each pair as a line, which gives grep-friendly output:
fx order.json paths '@`${x.path} = ${JSON.stringify(x.value)}`' listOutput (abbreviated):
.id = "ord_1042"
.status = "shipped"
.customer.name = "Alice Chen"
.customer.email = "alice@example.com"
...
.items[1].qty = 2
.items[1].price = 24.5
.shipping.address.city = "Berlin"
.shipping.address.zip = "10115"
.shipping.tracking = null
.paid = trueFind the JSON Path of a Value or Key
With the paths helper, filter with ? and map with @. Find where a value occurs:
fx order.json paths '?.value === "Berlin"' '@.path' list.shipping.address.cityFind every occurrence of a key:
fx order.json paths '?.path.endsWith(".sku")'[
{ "path": ".items[0].sku", "value": "KB-01" },
{ "path": ".items[1].sku", "value": "MS-07" }
]Generate a JSON Schema Summary: Path to Type
Collapse array indices into [] and collect every type seen at each path. This reveals optional and nullable fields in an API response such as users.json:
{
"page": 1,
"users": [
{"id": 1, "name": "Alice", "phone": "+49 30 1234", "roles": ["admin"]},
{"id": 2, "name": "Bob", "phone": null, "roles": []},
{"id": 3, "name": "Carol", "roles": ["dev", "ops"], "team": {"name": "infra"}}
]
}fx users.json 'x => {
const schema = {};
const visit = (v, p) => {
const type = v === null ? "null" : Array.isArray(v) ? "array" : typeof v;
if (p) (schema[p] ??= new Set()).add(type);
if (Array.isArray(v)) v.forEach(c => visit(c, `${p}[]`));
else if (type === "object") for (const k in v) visit(v[k], `${p}.${k}`);
};
visit(x, "");
return Object.fromEntries(
Object.entries(schema).map(([p, t]) => [p, [...t].join(" | ")])
);
}'Output:
{
".page": "number",
".users": "array",
".users[]": "object",
".users[].id": "number",
".users[].name": "string",
".users[].phone": "string | null",
".users[].roles": "array",
".users[].roles[]": "string",
".users[].team": "object",
".users[].team.name": "string"
}Recursively Visit Every Key With walk
walk(fn) is a recursive map: fn(value, key) is called for every value in the tree, where key is the object key or array index, and the result replaces that value. Children are visited before their parents. Use the key argument to list every unique key at any depth, keeping only strings since array indices are numbers. Return v unchanged so walk does not alter the data:
fx order.json 'x => {
const keys = new Set();
walk((v, k) => (typeof k === "string" && keys.add(k), v))(x);
return [...keys].sort().join(" ");
}'Output:
address city customer email id items name paid price qty shipping sku status tracking zipSearch JSON Paths Interactively
Run fx order.json with no other arguments to open the TUI:
- @ starts a fuzzy search over all JSON paths and jumps to the first match, so typing a fragment like
cityis enough to reach a deeply nested key. - . opens the query input. Type a path; fx suggests keys as you type, and tab accepts the suggestion. Press enter to show only that value and esc to go back.
- / searches the document with a regex; n / N go to the next or previous match.
- P prints the value under the cursor to stdout. See key bindings for the full list.